Reputation: 37
While trying to export list to dictionaries data to excel using python pandas, only values are getting exported not the keys. Can anyone please help to modify the code so that the keys should also get printed along with values in excel
import xlsxwriter
import pandas as pd
data = [{1:1, 2:2, 3:3}, {1:1, 2:2, 3:3}, {1:1, 2:2, 3:3}]
df = pd.DataFrame.from_dict(data)
df = df.transpose()
writer = pd.ExcelWriter('Pandas-Example.xlsx', engine='xlsxwriter')
df.to_excel(writer, 'Sheet1', index=False)
writer.save()
Upvotes: 0
Views: 154
Reputation: 153460
You are not exporting the index of the dataframe which are your keys by using index=False
.
Remove index=False, index=True is the default.
df.to_excel(writer, 'Sheet1')
Upvotes: 1