I have a Dataframe with two columns named volume qty and volume price. I want to create a third column based on above mentioned columns.
If both the volume qty and volume price columns have value or both are blank or empty then third column should have value "YES". If one of columns as value and another is empty or blank then I want third column should have value "NO" Eg
volume qty volume price column x
20 100 YES
YES
30 NO
200 NO
Is there way I can achieve this with any inbuild functions.
CodePudding user response:
You can use numpy.select
:
Case 1: When columns have empty(NaN) values:
In [152]: df
Out[152]:
volume qty volume price
0 20.0 100.0
1 NaN NaN
2 30.0 NaN
3 NaN 200.0
In [152]: import numpy as np
In [153]: conds = [df['volume qty'].notna() & df['volume price'].notna(), df['volume qty'].isna() & df['volume price'].isna(), df['volume qty'].isna() | df['volume price'].isna()]
In [154]: choices = ['YES', 'YES', 'NO']
In [156]: df['column x'] = np.select(conds, choices)
In [157]: df
Out[157]:
volume qty volume price column x
0 20.0 100.0 YES
1 NaN NaN YES
2 30.0 NaN NO
3 NaN 200.0 NO
Case 2: When columns have blank values:
In [167]: df
Out[167]:
volume qty volume price
0 20 100
1
2 30
3 200
In [164]: conds = [~df['volume qty'].eq('') & ~df['volume price'].eq(''), df['volume qty'].eq('') & df['volume price'].eq(''), df['volume qty'].eq('') | df['volume price'].eq('')]
In [165]: choices = ['YES', 'YES', 'NO']
In [168]: df['column x'] = np.select(conds, choices)
In [169]: df
Out[169]:
volume qty volume price column x
0 20 100 YES
1 YES
2 30 NO
3 200 NO