Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python sum a column's value with condition

I have the below dataframe. I would like to return a second column that is the sum of every item in the column with a condition: only those larger than -1.

input

   Price
0    12
1    14
2    15
3    10
4     2
5     4
6    -5
7    -4
8    -3
9    -5
10   16
11   15

output

   Price     Total Sum
0    12          88
1    14
2    15
3    10
4     2
5     4
6    -5
7    -4
8    -3
9    -5
10   16
11   15
like image 527
sirimiri Avatar asked Oct 24 '25 03:10

sirimiri


1 Answers

To get the sum of positive values in the column, use the appropriate condition

import pandas as pd

df = pd.DataFrame({'price': [12, 14, 15, 10, 2, 4, -5, -4, -3, -5, 16, 15]})
total = df.loc[df['price'] > 0, 'price'].sum()
print(total)  # 88

That isn't a good idea to set a column with values not relative to the other row param, here one single value. But to get the logic

# you need to pad with zeros, if you not you'll have 88 at every row
df['total'] = [total] + [0] * (len(df) - 1)
print(df)
    price  total
0      12     88
1      14      0
2      15      0
3      10      0
4       2      0
5       4      0
6      -5      0
7      -4      0
8      -3      0
9      -5      0
10     16      0
11     15      0
like image 126
azro Avatar answered Oct 25 '25 18:10

azro