1
I’m trying to make dataframes and pass them to excel, but when I do a dataframe with more than one header line, it skips a line before plotting the data in the spreadsheet (leaves an empty line). It seems that the problem is tied to the fact of my argument Columns be a list of lists, because it doesn’t happen in dataframes where Columns is a single list. Does anyone have any suggestions on how to resolve this, take away these empty lines?
import pandas as pd
import numpy as np
def multiple_dfs(df_list, sheets, file_name, spaces):
writer = pd.ExcelWriter(file_name,engine='xlsxwriter')
row = 0
for dataframe in df_list:
dataframe.to_excel(writer, sheet_name=sheets, startrow=row, startcol=0)
row = row + len(dataframe.index) + spaces + 1
writer.save()
array1 = [['one', 'one', 'one', 'two', 'two', 'two'],['a', 'b', 'c', 'a', 'b', 'c']]
df1 = pd.DataFrame(np.random.randn(5, 6),columns=array1)
array2 =[[1, 2, 3], ['Sharon', 'Nick', 'Bailey'],[22,23,24]]
midx = pd.MultiIndex.from_arrays(array2,
names =('Number', 'Names', 'Ages'))
df2 = pd.DataFrame(np.random.randn(5, 3),columns=midx)
array3 = ['one', 'one', 'one', 'two', 'two', 'two']
df3 = pd.DataFrame(np.random.randn(5, 6),columns=array3)
df_lista = [df1, df2, df3]
multiple_dfs(df_lista,'Frames','Frames.xlsx',4)
Change anything if you put
row = 1
?– Paulo Marques
He would start printing the dataframes from the second line of the spreadsheet
– gabrielouverney
Yes, exactly. I understood that the problem is that you are skipping a line between the header and the data. But, you tried
row = 1
?– Paulo Marques
I just tried, it didn’t work
– gabrielouverney