嘿,我有这样的数据框
Descriptions
1. i am going in car
2. He will go for match
3. D-1799
4. nothing better
5. SS-1428
6. going towards the office
7. GTY6QD12
另一个Dataframe具有上面dataframne中提到的代码,并在下一栏中进行描述。
code Description
1. D-1799 he is moving now
2. SS-1428 they will have some
3. GTY6QD12 is it well now
我希望第一个数据框填充基于代码的第二个数据框的代码描述。所需的结果应该是这样的。
Descriptions
1. i am going in car
2. He will go for match
3. he is moving now
4. nothing better
5. they will have some
6. going towards the office
7. is it well now
使用Series.replace
通过Series
从df2
所创造DataFrame.set_index
和选择Descriptions
列:
s = df2.set_index('code')['Description']
df1['Descriptions'] = df1['Descriptions'].replace(s)
print (df1)
Descriptions
0 i am going in car
1 He will go for match
2 he is moving now
3 nothing better
4 they will have some
5 going towards the office
6 is it well now
如果需要子字符串替换,请添加regex=True
:
df1['Descriptions'] = df1['Descriptions'].replace(s, regex=True)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句