我想在前一列的基础上添加一个新的DataFrame列,这样,如果前一列元素与列表值匹配,就可以更改该值

2024-10-01 11:35:00 发布

您现在位置:Python中文网/ 问答频道 /正文

输入df

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

映射列表以输入df

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

输出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

Tags: df列表imgindexcol1fruitshirtball
2条回答

试试这个

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

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

为了防止拆分不匹配(例如:A_Fruit_100),您可以extract然后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

相关问题 更多 >