Gurpreet
Gurpreet

Reputation: 37

Key is not getting printed to excel from list of dictionaries using python pandas

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

Answers (1)

Scott Boston
Scott Boston

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

Related Questions