Pandas:在数据帧单元中求和唯一值

2024-04-19 19:23:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下简略的数据帧:

url='https://raw.githubusercontent.com/108michael/ms_thesis/master/mpl.Bspons.merge.2'

df=df.reset_index()
df.head()

df['billsum'] = df.groupby(['date', 'catcode','disposition', 'id.fec']).bills.transform('sum')
    catcode     date    bills   id.fec  disposition     billsum
0   B1000   2013    hr2575-113  H0IN09070   support     hr2575-113
1   B2000   2013    hr2575-113  H0IN09070   support     hr2575-113
2   B3000   2013    hr2575-113  H0IN09070   support     hr2575-113hr2575-113
3   B6000   2013    hr2575-113  H0IN09070   support     hr2575-113hr2575-113hr2575-113hr2575-113hr2575...
4   B2000   2007    s1782-110   S8WI00026   oppose  s1782-110

问题是,我只想对列billsum求和,而不是输出所有的账单。当我尝试使用

^{pr2}$

我得到以下信息

    catcode     date    bills   id.fec  disposition     billsum
0   B1000   2013    hr2575-113  H0IN09070   support     1
1   B2000   2013    hr2575-113  H0IN09070   support     1
2   B3000   2013    hr2575-113  H0IN09070   support     2
3   B6000   2013    hr2575-113  H0IN09070   support     5
4   B2000   2007    s1782-110   S8WI00026   oppose  1

df.to_csv('mpl.billsum', index_col=0)

但我需要的是唯一值的总和。在我上面的简短df中没有唯一的值,但是在较大的数据库中有唯一的值。有人对此有什么想法吗?在


Tags: idsupportdfdateindexmplfecdisposition