3
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?

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?

2 Answers2

6

It seems you need 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')
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    Really hard question, because I have no your data. Also I cannot test `send_request` :( – jezrael Oct 16 '17 at 13:18
  • OK, so can you change your question with multiple `dfs` for testing? Because I have no data and dont know desired output. Check [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – jezrael Oct 16 '17 at 13:24
  • I see you r modify answer, but how I can test offsets? I need 3-4 dataframes as input to function. thanks. – jezrael Oct 16 '17 at 13:30
  • Ya, unfortunately without multiple dataframes and desired outpyut is it not possible, also `row` is unknown variable. – jezrael Oct 16 '17 at 13:44
  • Yes, but I have no idea what is number of columns, rows, what is desired output. So I need your sample data, sorry :( Try to imagine you want answer only with information in question - I think it is not possible. I can guessing, but always better is nice sample data with desired output. – jezrael Oct 16 '17 at 13:55
  • Thanks, I try `print (multiple_dfs('file.xlsx','aaa'))` and return errors :( – jezrael Oct 16 '17 at 14:08
  • I never do it with `openpyxl` :( – jezrael Oct 16 '17 at 14:25
  • Don't worry, I just ask ;) –  Oct 16 '17 at 14:26
  • Do you need `writer = pd.ExcelWriter(file_name, engine='openpyxl')`? Because for me it does not work, only working `writer = pd.ExcelWriter(file_name)` :( Maybe because windows? I have no idea. – jezrael Oct 16 '17 at 14:51
  • Remove it, it is not a big deal –  Oct 16 '17 at 15:08
  • You forget for desire output, so I guess a bit and modify answer. Please check it. – jezrael Oct 16 '17 at 15:16
0

Just ran into the same error. My code worked fine on my computer which was like this:

test_dict = {'x': '123', 'y': '456', 'z': '456'}
df=pd.DataFrame(test_dict.items(),columns=['col1','col2'])

However, it did not work on another platform. which gave me the error ValueError: DataFrame constructor not properly called! just like the original question.

I tried below code by simply adding the list() around the dictionary item,

df=pd.DataFrame(list(test_dict.items()),columns=['col1','col2'])

and it worked smoothly!

Hopefully, this answer can help whoever ran into a similar situation like me.

dayaoyao
  • 120
  • 2
  • 9