user12729159
user12729159

Reputation: 141

Aggregate the column values and keep the values greater than certain number

Given a dataframe looks like this:

Unique_id  Value
105590  0.000627
105590  0.000650
105590  0.000648
105590  0.000650
......  ........
......  ........
106690  0.000652
106690  0.000634
106690  0.000632

There are unique_ids and every id has 288 values (rows)

I have tried this

df3 = df.groupby('Unique_id').sum().reset_index()

but it only aggregate the values of unique ids.

How can this be done?

Thanks!

Upvotes: 0

Views: 676

Answers (2)

jezrael
jezrael

Reputation: 862431

If need filter aggregated values use Series.between:

s = df.groupby("Unique_id")["Value"].sum()

df1 = s[s.between(15, 20)].reset_index(name='summed')

If need filter original DataFrame faster way use GroupBy.transform and then filter original DataFrame:

df2 = df[df.groupby("Unique_id")["Value"].transform('sum').between(15, 20)]

Upvotes: 2

not_speshal
not_speshal

Reputation: 23146

I think this is what you want:

df.groupby("Unique_id").filter(lambda x: (x["Value"].sum()>15)&(x["Value"].sum()<20))

Upvotes: 3

Related Questions