Python ValueError:未正确调用 DataFrame 构造函数!与熊猫

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46771235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:50:34  来源:igfitidea点击:

ValueError: DataFrame constructor not properly called! with pandas

pythonpandas

提问by jezrael

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)

I have the following error ValueError: DataFrame constructor not properly called!, but I can't say what is the problem. Any suggestions?

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

I want to put a title at the top of a worksheet, but I don't know if it is the best practice. Could anyone be able to suggest me something with pandas?

我想在工作表的顶部放置一个标题,但我不知道这是否是最佳做法。谁能给我推荐一些关于熊猫的东西?

回答by jezrael

It seems you need list:

看来你需要list

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

EDIT:

编辑:

It seems you need:

看来你需要:

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')