Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+4 votes
5 views
in Python by (2.6k points)
edited by

def multiple_dfs(xyz_file, sheet, *args):
row=2 writer = pd.ExcelWriter(xyz_file, engine='openpyxl') df = pd.DataFrame({'user': ['A', 'B', 'C'], 'income': [50000, 60000, 53000]})df2 = pd.DataFrame({'amount': [200, 600, 2000],
'income': [50000, 60000, 53000]})df2.to_excel(author, sheet, startrow=row, index=False)df3.to_excel(author, sheet, startrow=0, startcol=0, header=None, \index=False)

 Running this command throwing an error ValueError: DataFrame constructor not properly called! and I am unable to figure it out, can someone help me over this?

2 Answers

+1 vote
by (46k points)
edited by

Add list:

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

+2 votes
by (32.3k points)
edited by

Try to do something like this:

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

You can use the following video tutorials to clear all your doubts:-

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...