Jan-23-2018, 06:08 PM
data={'id': [0,1,2,3], 'A':[1.1, 1.2, 1.3, 1.4], 'B':[1.0, 1.1, 1.2, 1.3], 'C':[1.1, 1.1, 1.3, 1.3], 'D':[10.0, 20.0, 30.0, 40.0]} df0=pd.DataFrame(data) df1=df0.set_index('id') def get_sum(df): sum1= df.apply(lambda x: x['C'] * x['D'] if x['C'] >= x['A'] else (-1.0 * x['C'] * x['D'])) return(sum1)Hello:
I want to write a function to return a conditional results.
The condition is:
from my dataframe df1, if the value of column 'C' is equal or greater than column 'A', then return the multiplication of column 'C' and column 'D'; if if the value of column 'C' is equal or smaller than column 'B', then return the negative multiplication of column 'C' and column 'D'; if the value of column 'C' is between column 'A' and column 'B', then return 0.0
In the above example,
I want to return the following results:
[11.0, -22.0, 39.0, -52.0]
However, I don't know how to write the 3 conditions in lambda expression; besides, my above code didn't work.
If you have other idea can do this without lambda is also OK, please advice.
Thanks,