Given a dataframe I want to check whether DS1.ColA or DS1.ColB contains "Type 1" and if it does, I want to insert the corresponding DS1.Val to column Value. The same goes for DS2, check if DS2.ColA or DS2.ColB contains "Type 1" and if it does, I want to insert the corresponding DS2.Val to column Value.
df = pd.DataFrame(
{
'ID': ['AB01', 'AB02', 'AB03', 'AB04', 'AB05','AB06'],
'DS1.ColA': ["Type 1","Undef",np.nan,"Undef",
"Type 1", ""],
'DS1.ColB': ["N","Type 1","","",
"Y", np.nan],
'DS1.Val': [85,87,18,94,
81, 54],
'DS2.ColA': ["Type 1","Undef","Type 1","Undef",
"Type 1", ""],
'DS2.ColB': ["N","Type 2","","",
"Y", "Type 1"],
'DS2.Val': [45,98,1,45,66,36]
}
)
var_check = "Type 1"
ds1_col_check = ["DS1.ColA","DS1.ColB","DS1.Val"]
ds2_col_check = ["DS2.ColA","DS2.ColB","DS2.Val"]
The last element of ds1_col_check and ds2_col_check is always the element to place in the new column(There could be more columns to checks in the list).The end result df should look like this. How do I achieve this in python?
