I want to add a new DataFrame column based on previous column such that if previous column element matches with list value, change the value

Viewed 99

Input df

Index       col1
0     Img    
1     Fruit  
2     Img    
3     Ball    
4     Ball    
5     Fruit    
6     shirt    
7     Fruit 

Map list to input df

list1 = ['Img_A_10', 'Fruit_A_100', 'Ball_B_120']

Output df

     col1      col22
0     Img    Img_A_10
1     Fruit  Fruit_A_100
2     Img    Img_A_10
3     Ball   Ball_B_120
4     Ball   Ball_B_120
5     Fruit  Fruit_A_100  
6     shirt  shirt         
7     Fruit  Fruit_A_100
2 Answers

try this,

df['col2'] = df.col1.map({k.split("_")[0]: k for k in list1}).fillna(df.col1)

or

df['col2'] = df.col1.replace({k.split("_")[0]: k for k in list1})

df
Out[93]: 
    col1         col2
0    Img     Img_A_10
1  Fruit  Fruit_A_100
2    Img     Img_A_10
3   Ball   Ball_B_120
4   Ball   Ball_B_120
5  Fruit  Fruit_A_100
6  shirt        shirt
7  Fruit  Fruit_A_100

just in case splits doesn't match (example : A_Fruit_100) , you can extract then replace

s = pd.Series(list1)
d = dict(zip(s.str.extract('('+'|'.join(df['col1'])+')',expand=False),s))
df['col22'] = df['col1'].replace(d)

print(df)
        col1        col22
Index                    
0        Img     Img_A_10
1      Fruit  Fruit_A_100
2        Img     Img_A_10
3       Ball   Ball_B_120
4       Ball   Ball_B_120
5      Fruit  Fruit_A_100
6      shirt        shirt
7      Fruit  Fruit_A_100
Related