How can I get conditional assignment in pandas based on the values of two columns? Conceptually, something like the following:
Column_D = Column_B / (Column_B + Column_C) if Column_C is not null else Column_C
Specific example:
import pandas as pd
import numpy as np
df = pd.DataFrame({'b': [2,np.nan,4,2,np.nan], 'c':[np.nan,1,2,np.nan,np.nan]})
b c
0 2.0 NaN
1 NaN 1.0
2 4.0 2.0
3 2.0 NaN
4 NaN NaN
I want to have a new column d, the result of which is to divide the column bby the sum band c, if cnot null, otherwise the value should be the value in the column c. Something conceptually looks like this:
df['d'] = df['b']/(df['b']+df['c']) if not df['c'].isnull() else df['c']
desired result:
b c d
0 2.0 NaN NaN
1 NaN 1.0 1.0
2 4.0 2.0 0.66
3 2.0 NaN NaN
4 NaN NaN NaN
How can i achieve this?
source
share