4

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
4
  • 4
    a new column with one value, that isn't a good idea Commented Nov 28, 2021 at 11:57
  • Please always provide a reproducible example : pytohn code of the values at least Commented Nov 28, 2021 at 11:58
  • df["Positive"] = max(df["Price"], 0) then result = df["Positive"].sum() Commented Nov 28, 2021 at 11:58
  • sum([c for c in col if c >= 0]) ? Commented Nov 28, 2021 at 12:01

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

+10, but instead of making a manual list, could create the column like this: df.loc[0, 'total'] = total
0

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())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.