How to sum dataframe in pandas for more than 5

Viewed 78

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
2 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

I'd do it like this:

above = df.files[df.Days >= 5].sum()
df[df.Days < 5].append({'Days': '5+', 'files': above}, ignore_index=True)

It gives:

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