Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sum dataframe in pandas for more than 5

in my days column if my number is more than 5 I want to sum that column

Input:

Days  files 
1      10
3      20
4      30
5      40
6      50

Required output:

Days  files 
1      10
3      20
4      30
5+     90
like image 796
Shinde Avatar asked Jan 24 '23 20:01

Shinde


1 Answers

You can try series.clip to clip the upperbound in the series , then do a groupby and sum:

out = df['files'].groupby(df['Days'].clip(upper=5)).sum().reset_index()
print(out)

  Days  files
0    1     10
1    3     20
2    4     30
3    5     90

If you really want to change anything above 5 into a str type , you can jst replace the 5 using the above logic:

out = df['files'].groupby(df['Days'].clip(upper=5).replace(5,'5+')).sum().reset_index()
print(out)

  Days  files
0    1     10
1    3     20
2    4     30
3   5+     90
like image 200
anky Avatar answered Jan 29 '23 11:01

anky