I have data frame as shown below. I need to compare column in a data frame with the string and creating a new column.
DataFrame:
col_1
AB_SUMI
AK_SUMI
SB_LIMA
SB_SUMI
XY_SUMI
If 'AB','AK','SB' are present in col_1 it should create a new column with their respective values otherwise '*' should come in the column value.
expected output:
col_1 new_col
AB_SUMI AB
AK_SUMI AK
SB_LIMA SB
SB_SUMI SB
XY_SUMI *
I have tried with below code but not worked out.
list=['AB','AK','AB']
for item in list:
if df['col1'].str.contains(item).any():
df['new']=item
please help me in this regard. Thanks in advance