openpyxl数据透视表和pandas数据透视表

时间:2020-02-11 06:16:38

标签: python pandas data-science openpyxl

如何使用openxlpy库创建等效于以下pandas数据透视表功能的数据透视表?

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) 

1 个答案:

答案 0 :(得分:0)

这能达到您想要的结果吗?

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")

输出

Excel Output