I have data frame shown below. df:
col_1 col_2
EDU facebook
EDU google
EDU google_usa
EDU tabula
EDU xyz
EDU abc
IAR facebook
IAR google
If col_1 has 'EDU' and col_2 has 'facebook', 'google' new_col should have same string i.e facebook and google , if col_2 contains 'google_usa',tabula' new_col should contains 'gusa' and if col_2 has any other strings ne_col should have others in the same data frame.
If col_1 has 'IAR'and col_2 has 'facebook' new_col should have facebook and for any other string in the col_2 it should contain 'other' in the same data frame.
Expected output:
col_1 col_2 new_col
EDU facebook facebook
EDU google google
EDU google_usa gusa
EDU tabula gusa
EDU xyz others
EDU abc others
IAR facebook facebook
IAR google others
I tried below code but not worked out.Please help me in this regard. thanks in advance.
if df['col_1'].str.contains('EDU').any():
df['new_col'] = ['facebook' if 'facebook' in x else
'google' if 'google' == x else
'gcusa_tb' if 'taboola' in x else
'gcusa_tb' if 'google_cusa' in x else
'Others' for x in df['col_2']]