Create Pivot table in an excel file using python

Viewed 32

Can anyone guide me on creating pivot table in an excel file using python? I have tried some methods. But did not work

1 Answers

can you try this:

go to terminal install===>

 pip install xlsxwriter

 pip install pandas

Example code:

import pandas as pd
import numpy as np
import xlsxwriter

new_technologies= {
    'Courses':["Python","Java","ASP.Net","Python","C"],
    'Fees' :[22000,25000,23000,28000,26000],
    'Duration':['30days','50days','30days',  '38days', '30days']
          }

df = pd.DataFrame(new_technologies)


pivot = df.pivot_table(index =['Courses','Duration'],
                       values =['Fees'],
                       aggfunc ='sum')
print(pivot)
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
pivot.to_excel(writer, sheet_name='welcome', index=True)
writer.save()

Related documentation:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.pivot_table.html

Related