gpt4 book ai didi

python - pandas 中的连接表

转载 作者:行者123 更新时间:2023-12-01 09:08:47 24 4
gpt4 key购买 nike

我正在尝试使用 pandas 在我的 Excel 工作表上执行一些操作,并使用以下代码连接表格。

我编写了以下函数来连接字符串并将其保存在数据框中。

def row_split(row, product, measure, price): 


out_dict = dict.fromkeys(row.index)

for i in out_dict.keys():
out_dict[i] = []

cols = [x for x in out_dict.keys() if x not in [product, measure, price]]

temp_list = []
for i, j in zip(row[measure],row[price]):
try:
temp_list.append([row['Products'] + ' ' + i, i, int(j)])
except ValueError:
temp_list.append([row['Products'], np.nan, np.nan])

for i in cols:
for x in row['Measure']:
out_dict[i].append(row[i])


for x in temp_list:
for i, j in zip([product, measure, price], x):
out_dict[i].append(j)

return pd.DataFrame(out_dict)

split_tables = []
for index, row in (tab1).iterrows():
split_tables.append(row_split(row, 'Products', 'Measure', 'Price'))

temp = pd.concat(split_tables[1], split_tables[2])

final_df = reduce(lambda x, y: pd.concat([x, y]), split_tables)

但它返回一个错误-

File "<ipython-input-8-b1c137cbfc9c>", line 48, in <module>
temp = pd.concat(split_tables[1], split_tables[2])

File "/anaconda3/lib/python3.6/site-packages/pandas/core/reshape/concat.py", line 225, in concat
copy=copy, sort=sort)

File "/anaconda3/lib/python3.6/site-packages/pandas/core/reshape/concat.py", line 241, in __init__
'"{name}"'.format(name=type(objs).__name__))

TypeError: first argument must be an iterable of pandas objects, you passed an object of type "DataFrame"

有人可以帮我解决这个问题吗?

最佳答案

您应该传递一个可迭代的 pandas 对象。不要单独进行它们。

尝试:

temp = pd.concat(split_tables)

或者如果你真的只想要前两个

temp = pd.concat(split_tables[:2])

关于python - pandas 中的连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51830218/

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