python - pandas pivot_table Margin=True 的问题没有给我正确的答案
问题描述
pd.pivot_table(subset_one,index=["Institution_Types"],values=["Current_Balance"],aggfunc=([len, np.mean, max, min]),\ margins=True, margins_name='Total').reset_index()\ .rename(columns={'len': 'Loan Count', 'mean':'Average', 'max':'Max','min':'Min'})
总列没有给出正确答案。查看我的结果
解决方案
或者,您可以尝试使用数据透视表,Margin=False
然后添加一行Total,如下所示:
pd.pivot_table(
subset_one,
index=["Institution_Types"],
values=["Current_Balance"],
aggfunc=([len, np.mean, max, min]),
margins=False,
).reset_index().rename(
columns={"len": "Loan Count", "mean": "Average", "max": "Max", "min": "Min"}
)
df = df.append(df.sum(axis=0), ignore_index=True)
df.iloc[-1, 0] = "Total"
推荐阅读
- c# - 如何最好地处理 C# 中的位数据?
- css - 网格显示列的宽度不相等
- android - 如何抑制 Gradle 自动将 vectorDrawable 转换为 PNG?
- c++ - C++ 如何从 ifstream 和 getline() 中获取子字符串
- css - 在 CSS ::selection 中保持继承背景
- c# - 使用应用程序池标识在 IIS 上的 ASP.NET 中查询 Win32_Printer 时权限被拒绝
- html - 将框重新对齐为 2x3
- javascript - 替换 src 中的拇指大小
- mongodb - 我需要为从 mongodb 读取的春季批次运行测试
- wordpress - 如果用户未登录但不重定向 googlebot,则重定向到登录页面