[英]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
Input df输入 df
Index col1
0 Img
1 Fruit
2 Img
3 Ball
4 Ball
5 Fruit
6 shirt
7 Fruit
Map list to input df将列表映射到输入 df
list1 = ['Img_A_10', 'Fruit_A_100', 'Ball_B_120']
Output df输出 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
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
以防万一拆分不匹配(例如: 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.