I have a df and I want to insert a new column which checks ColA, ColB, and ColC. The new column will have value 'Yes' unless any of those have corresponding values for the Cols above are met( provided in the dictionary). How do I achieve this in python?
import pandas as pd
import numpy as np
to_check= {'ColA':'Yes', 'ColB':'Y','ColC':'True'}
df = pd.DataFrame(
{
'ID': ['AB01', 'AB02', 'AB03', 'AB04', 'AB05','AB06','AB07','AB08'],
'ColA': ["Yes","No",np.nan,"No",
"Yes", "","", "No"],
'ColB': ["N","","Y","",
"Y", np.nan,"", "N"],
'ColC': [np.nan,"True","False",np.nan,"False",
"True", "",""],
'Type': [85,98,84,70,50,np.nan,85,74]
}
)
The end result should be like this with new column named 'Result' and I have to use the dictionary in the code:
CodePudding user response:
You can use if not any
of the rows match the dictionary value:
c = df[to_check.keys()].eq(to_check).any(1)
df['Result'] = np.where(c,'','Yes')
print(df)
ID ColA ColB ColC Type Result
0 AB01 Yes N NaN 85.0
1 AB02 No True 98.0
2 AB03 NaN Y False 84.0
3 AB04 No NaN 70.0 Yes
4 AB05 Yes Y False 50.0
5 AB06 NaN True NaN
6 AB07 85.0 Yes
7 AB08 No N 74.0 Yes