首页 > 解决方案 > How to sum data and join columns using Pandas

问题描述

I want to sum values in data

from this:

enter image description here

to this:

enter image description here

but the problem is, the data doesn't add up but they like 11, 1111

here is my code:

df_data.insert(loc=2, column='Jumlah', value='1')
df_data.pivot_table(index='Kecamatan', columns='Status', aggfunc='sum', fill_value=0)

And how I can make the columns only KECAMATAN NEGATIF ODP ODPSELESAI OTG PDP

Thank you guys.

标签: python-3.xpandas

解决方案


请注意,插入列 ( Jumlah ) 的值是一个字符串。在下一条指令中,您将尝试生成一个对该列求和的数据透视 表。

但是,如果您尝试对文本值求和,它实际上意味着concatenation

为了解决问题,将第一条指令更改为:

df_data.insert(loc=2, column='Jumlah', value=1)

即删除1周围的撇号。

然后此列将是int类型,并将根据您的需要进行汇总。


推荐阅读