Home > Software design >  python sum a column's value with condition
python sum a column's value with condition

Time:11-28

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

CodePudding user response:

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

CodePudding user response:

I would not recommend making another column with only one value in it. To get the sum of all prices you could do:

all_prices = []
df = pandas.read_csv("prices.csv")
df_price = df["price"]
print(df_price.sum())
  • Related