I am trying to sum two columns of the DataFrame to create a third column where the value in the third column is equal to the sum of the positive elements of the other columns. I have tried the below and just receive a column of NaN values
JavaScript
x
8
1
df = pd.DataFrame(np.array([[-1, 2], [-2, 2], [1, -3], [1, -4], [ -2 , -2]]),
2
columns=['a', 'b'])
3
4
df['Sum of Positives'] = 0
5
6
df['Sum of Positives'] = df.loc[df.a > 0 ,'a'] +df.loc[df.b >0 , 'b']
7
8
DataFrame:
Advertisement
Answer
You can use df.mask
here and fill value less than 0 i.e negative value with 0 and do df.sum
over axis 1.
JavaScript
1
9
1
df['sum of pos'] = df.mask(df<0, 0).sum(axis=1)
2
3
a b sum of pos
4
0 -1 2 2
5
1 -2 2 2
6
2 1 -3 1
7
3 1 -4 1
8
4 -2 -2 0
9
Few NumPy hacks that are useful here.
Using
np.copyto
JavaScript141t = np.copy(df.values)
2np.copyto(t, 0, where=df.values<0)
3df['sum of pos'] = t.sum(axis=1)
4
Using
np.where
JavaScript121df['sum of pos'] = np.where(df.values<0, 0, df.values).sum(axis=1)
2
Using
np.clip
JavaScript121df['sum of pos'] = np.clip(df.values, 0, None).sum(axis=1)
2
Using
np.ma.array
JavaScript131m = np.ma.array(df.values, mask=df.values<0, fill_value=0)
2df['sum of pos'] = m.filled().sum(axis=1)
3