Search code examples
python-3.xpandaspandas.excelwriter

How to unstack two columns from groupby()


I have the below code to count how many product does each sale person sold in different locations

df = df.groupby(['location','sales person','item']).size().unstack().fillna(0) 
df.to_excel(r'c:\users\Tom\Project\info.xlsx')

The output I have is as below, but I need to fill the empty cell in column A, that means I want to see 'East' on row 3 and 'West' on row 5. Is there a way to do it? Thanks

enter image description here


Solution

  • You can try:

    df.reset_index().to_excel(r'c:\users\Tom\Project\info.xlsx', index=False)