简体   繁体   中英

Python : Pandas Sum with more than one condition

I want to know how to do a sum on a column depending of the value of another column (0 or 1)

   id   area PP
a   0,95999998  0
a   0,44    1
b   1,6900001   0
c   2   0
d   5,8499999   0
e   0,66000003  1

I can find the area for each id

surface_id = df.groupby("id")["area"].sum()

But what I also want is the area by id if PP = 1 to get something like this :

   id   area_PP
a   0,44
b   0   
c   0
d   0
e   0,66000003

Try:

df.eval('area * PP').groupby(df.id).sum()

This works by simply multiplying the area column by the PP column. 0 naturally cancels out the area appropriately.

I chose to use eval because it's cooler and for large data should be faster.

This does the same thing

(df.area * df.PP).groupby(df.id).sum()

One way using transform but longer

df['area_pp'] = df[df.PP == 1].groupby("id")["area"].transform('sum')
df.fillna(0, inplace = True)

    id  area        PP  area_pp
0   a   0,95999998  0   0
1   a   0,44        1   0,44
2   b   1,6900001   0   0
3   c   2           0   0
4   d   5,8499999   0   0
5   e   0,66000003  1   0,66000003

Another way:

total=df.groupby(['id', 'PP'])['area'].sum().reset_index(level=1)
total[total.PP==1].drop(axis=1, labels='PP')

If you were to just want the positively labeled instances in the output:

df = pd.DataFrame({'id': ('a', 'a', 'b', 'c', 'd', 'e'), 'area': (0.96, 0.44, 
1.69, 2., 5.85, 0.66), 'PP': (0, 1, 0, 0, 0, 1)})
df2 = df.where(df.PP==1).groupby('id')['area'].sum()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM