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/

10-12 04:57