gpt4 book ai didi

python - DataFrame Pandas 中的 ValueError

转载 作者:太空宇宙 更新时间:2023-11-03 14:34:02 24 4
gpt4 key购买 nike

我的目标是..

  • 如果数据框为空,我需要插入一行 index->value of the variable URLcolumns-> value of URL along with the sorted_list
  • 如果非空,我需要插入一行 index->value of the variable URLcolumns->sorted_list

我所做的是......我初始化了一个DataFrame self.pd然后对于具有上述值的每一行,我创建了一个本地 DataFrame 变量 df1并将其附加到 self.df

我的代码:

import pandas as pd

class Reward_Matrix:
def __init__(self):
self.df = pd.DataFrame()

def add(self, URL, webpage_list):
sorted_list = []
check_list = list(self.df.columns.values)
print('check_list: ',check_list)
for i in webpage_list: #to ensure no duplication columns
if i not in check_list:
sorted_list.append(i)
if self.df.empty:
sorted_list.insert(0, URL)
df1 = pd.DataFrame(0,index=[URL], columns=[sorted_list])
else:
df1 = pd.DataFrame(0,index=[URL], columns=[sorted_list])
print(df1)
print('sorted_list: ',sorted_list)
print("length: ",len(df1.columns))
self.df.append(df1)

但我收到以下错误:

Traceback (most recent call last):
File "...Continuum\anaconda3\lib\site-packages\pandas\core\internals.py", line 4294, in create_block_manager_from_blocks
placement=slice(0, len(axes[0])))]
File "...Continuum\anaconda3\lib\site-packages\pandas\core\internals.py", line 2719, in make_block
return klass(values, ndim=ndim, fastpath=fastpath, placement=placement)
File "...Continuum\anaconda3\lib\site-packages\pandas\core\internals.py", line 115, in __init__
len(self.mgr_locs)))
ValueError: Wrong number of items passed 1, placement implies 450

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "...eclipse-workspace\Crawler\crawl_core\src_main\run.py", line 23, in test_start
test.crawl_run(self.URL)
File "...eclipse-workspace\Crawler\crawl_core\src_main\test_crawl.py", line 42, in crawl_run
self.reward.add(URL, webpage_list)
File "...eclipse-workspace\Crawler\crawl_core\src_main\dynamic_matrix.py", line 21, in add
df1 = pd.DataFrame(0,index=[URL], columns=[sorted_list])
File "...Continuum\anaconda3\lib\site-packages\pandas\core\frame.py", line 352, in __init__
copy=False)
File "...Continuum\anaconda3\lib\site-packages\pandas\core\frame.py", line 483, in _init_ndarray
return create_block_manager_from_blocks([values], [columns, index])
File "...Continuum\anaconda3\lib\site-packages\pandas\core\internals.py", line 4303, in create_block_manager_from_blocks
construction_error(tot_items, blocks[0].shape[1:], axes, e)
File "...Continuum\anaconda3\lib\site-packages\pandas\core\internals.py", line 4280, in construction_error
passed, implied))
ValueError: Shape of passed values is (1, 1), indices imply (450, 1)

我不太熟悉 DataFrame 和 Pandas。我已经收到这个错误相当长一段时间了,当我在 StackOverflow 中遇到类似的问题时,我感到很困惑,因为我不明白我哪里出错了!

有人可以帮我吗?

最佳答案

我认为你需要删除[],因为否则会得到嵌套列表:

df1 = pd.DataFrame(0,index=[URL], columns=sorted_list)

示例:

sorted_list = ['a','b','c']
URL = 'url1'
df1 = pd.DataFrame(0,index=[URL], columns=sorted_list)
print (df1)
a b c
url1 0 0 0

df1 = pd.DataFrame(0,index=[URL], columns=[sorted_list])
print (df1)

>ValueError: Shape of passed values is (1, 1), indices imply (3, 1)

关于python - DataFrame Pandas 中的 ValueError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47089960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com