枢纽分析表无法获得与Excel相同的输出

问题描述

我在下面有一个数据框:

Year  Material  Total_amount Customer_code
2019  Table     20           00147
2019  Chair     25           00159
2019  Table     20           00160 
2020  Table     20           00159
2020  Spoon     2            00147     
2020  Fork      2            00147   

我试图找到不断回头的客户

cust = (pd.pivot_table(sd_df,values=['Total Amount','Customer Code'],index=['Year'],aggfunc=[np.sum,len]).reset_index()
         )
cust

我这样做了,但是这给了我

enter image description here

编辑:我试图获取总金额和客户代码总数。

感谢您的帮助。谢谢!

解决方法

您可以使用DataFrame.groupby

print(
    df.groupby('Year').agg(**{
        "Sum_Total_Amount": ('Total_amount','sum'),"Count_Cust_Code": ('Customer_code','nunique'),}).reset_index()
)

   Year  Sum_Total_Amount  Count_Cust_Code
0  2019                65                3
1  2020                24                2

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...