A data-frame that I want to do some Excel pivot-table alike summary, like the screenshot below.
import pandas as pd
data = {'People' : ["David","Kate","Kate","Kate","David","David","Kate","David","Kate"],
'Year': ["2016","2016","2016","2017","2017","2017","2016","2017","2016"],
'Project' : ["TN","DM","TN","DM","DJ","DM","DM","DM","TN"]}
df = pd.DataFrame(data)
df_1 = df.groupby(['People', 'Year', 'Project'])["Project"].count()
df_1 output as below:
Then I want to export all with no blank cells to Excel spreadsheet.
df_1.reset_index().to_excel('file.xlsx', index=False)
But it gives me error:
ValueError: cannot insert Project, already exists
I tried below also doesn't work:
df_1.reset_index().to_excel('file.xlsx', index=False)
What's the right way to write it? Thank you.
Try this:
df_1 = df.groupby(['People', 'Year', 'Project'])["Project"].count().reset_index(name="count")
df_1.to_excel('file.xlsx')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.