Pandas - Append Column With Sum Of Row Values (if Sum Is Even), Or Nan (if Odd)
I have a dataframe and I want a new column t with sum of other columns in the same row. Criteria is I want NaN if the sum is odd, and the sum if the value is even. df = pd.DataFram
Solution 1:
Using np.where
:
df['new'] = np.where(df.sum(1)%2==0, df.sum(1), np.nan)
df
p q r s new
0 1 8 7 2 18.0
1 8 5 9 4 26.0
2 1 -5 3 -2 NaN
Post a Comment for "Pandas - Append Column With Sum Of Row Values (if Sum Is Even), Or Nan (if Odd)"