I want to write the excel file with the header is
I have data and I want to fill out the excel file. Then the expected result is
This is my code but it does not show as my expected. Could you help me to fix it? Thanks
from pandas import DataFrame
id =['1','2']
width_man=['12','18']
height_man=['20','55']
age_man=['10','12']
width_woman=['15','11']
height_woman=['40','45']
age_woman=['11','15']df = DataFrame({'ID': id, 'WIDTH': width_man, 'HEIGHT': height_man, 'AGE': age_man, 'WIDTH': width_woman, 'HEIGHT': height_woman, 'AGE': age_woman,})
df.to_excel('table.xlsx', sheet_name='sheet1', index=False)
THIS IS OUTPUT OF MY CODE
Use below method,
import pandas as pdid =['1','2']
width_man=['12','18']
height_man=['20','55']
age_man=['10','12']
width_woman=['15','11']
height_woman=['40','45']
age_woman=['11','15']df1 = pd.DataFrame({'ID': id, 'WIDTH': width_man, 'HEIGHT': height_man, 'AGE': age_man})
df2 = pd.DataFrame({'ID': id, 'WIDTH': width_woman, 'HEIGHT': height_woman, 'AGE': age_woman})
writer = pd.ExcelWriter('table.xlsx')
df1.to_excel(writer, sheet_name='Sheet1', startrow=1, index=False)
df2.to_excel(writer, sheet_name='Sheet1',startrow=1,startcol=5, index=False)
worksheet = writer.sheets['Sheet1']
worksheet.write_string(0, 2, 'MAN')
worksheet.write_string(0, 6, 'WOMAN')writer.save()