openpyxl pivot table 和 pandas pivot table

openpyxl pivot table and pandas pivot table

如何使用等同于以下 pandas 数据透视 table 函数的 openxlpy 库创建数据透视 table?

import pandas as pd 

import numpy as np 


# creating a dataframe 

df = pd.DataFrame({'A': ['John', 'Boby', 'Mina', 'Peter', 'Nicky'], 

      'B': ['Masters', 'Graduate', 'Graduate', 'Masters', 'Graduate'], 

      'C': [27, 23, 21, 23, 24]}) 

table = pd.pivot_table(df, values ='A', index =['B', 'C'], 
                         columns =['B'], aggfunc = np.sum) 

这是否达到您想要的结果?

import pandas as pd 
import numpy as np 

# creating a dataframe 

df = pd.DataFrame({'A': ['John', 'Boby', 'Mina', 'Peter', 'Nicky'], 

      'B': ['Masters', 'Graduate', 'Graduate', 'Masters', 'Graduate'], 

      'C': [27, 23, 21, 23, 24]}) 

table = pd.pivot_table(df, values ='A', index =['B', 'C'], 
                         columns =['B'], aggfunc = np.sum)

table.to_excel("filename.xlsx")

输出