学习数据透视表的时候的几个问题,部分功能不能实现
原始的数据如图所以
我想解决的问题也和excel表对比着写出来了
我自己的代码如图所示
path_toushi = r'E:\work\学习透视表原始数据.xlsx'
data_toushi = pd.read_excel(path_toushi,sheet_name='元数据')
data_pivot_table = pd.pivot_table(data_toushi,
values=['消费','展现量'],
index=['业态','城市'],
aggfunc={'消费':'sum', '展现量':'count'},
margins=True,margins_name='总计')
data_choice = data_pivot_table.loc[pd.IndexSlice['互联网']]
data_choice.to_excel(r'E:\work\透视结果表.xlsx', sheet_name='数据')