gpt4 book ai didi

python - Pandas 的ValueError : DataFrame constructor not properly called!

转载 作者:行者123 更新时间:2023-11-28 22:23:55 32 4
gpt4 key购买 nike

def multiple_dfs(file_name, sheet, *args):
"""
Put multiple dataframes into one xlsx sheet
"""

row=2
writer = pd.ExcelWriter(file_name, engine='openpyxl')

df = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice'],
'income': [40000, 50000, 42000]})
df2 = pd.DataFrame({'amount': [100, 500, 1000],
'income': [40000, 50000, 42000]})
df3 = pd.DataFrame(['Title'])

df.to_excel(writer, sheet, startrow=row, index=False)
row += 2
df2.to_excel(writer, sheet, startrow=row, index=False)
df3.to_excel(writer, sheet, startrow=0, startcol=0, header=None, \
index=False)

我有以下错误 ValueError: DataFrame constructor not properly called!,但我不能说是什么问题。有什么建议吗?

我想将标题放在工作表的顶部,但我不知道这是否是最佳做法。谁能给我一些关于 Pandas 的建议?

最佳答案

看来你需要 list:

df2 = pd.DataFrame(['Test'])
print (df2)
0
0 Test

编辑:

看来你需要:

def multiple_dfs(file_name, sheet, *args):
"""
Put multiple dataframes into one xlsx sheet
"""

row=2
writer = pd.ExcelWriter(file_name)

df = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice'],
'income': [40000, 50000, 42000]})
df2 = pd.DataFrame({'amount': [100, 500, 1000],
'income': [40000, 50000, 42000]})
df3 = pd.DataFrame(['Title'])

df.to_excel(writer, sheet, startrow=row, index=False)
row += len(df) + 1
df2.to_excel(writer, sheet, startrow=row, index=False)
df3.to_excel(writer, sheet, startrow=0, startcol=0, header=None, \
index=False)

multiple_dfs('file.xlsx','Sheet1')

关于python - Pandas 的ValueError : DataFrame constructor not properly called!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46771235/

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