I have a dataframe which looks like this
pd.DataFrame({'A': [5, 2, -3],
...: 'B': [9, -1, 7],
...: 'C': [-5, 2, -6]})
Out[28]:
A B C
0 5 9 -5
1 2 -1 2
2 -3 7 -6
I would like to do the following
x > 0 -> (1+x)
x < 0 -> (1-x)^-1 (to the power of -1)
and then sum up all the columns.
resulting dataframe should look like this
A B C D Logic
0 5 9 -5 16.1667 (1+5)+(1+9)+((1-(-5))^-1)
1 2 -1 2 6.5 (1+2)+((1-(-1))^-1)+(1+2)
2 -3 7 -6 8.3929 ((1-(-3)^-1)+(1+7)+((1-(-6))^-1)
Here you go with np.where
:
df['D'] = np.where(df>0, 1+df, 1/(1-df)).sum(1)
Output:
A B C D
0 5 9 -5 16.166667
1 2 -1 2 6.500000
2 -3 7 -6 8.392857
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments