简体   繁体   English

根据列中的值填充另一个 dataframe 的值

[英]Filling values from another dataframe based of value in a column

Hey I have dataframe like this嘿,我有这样的 dataframe

   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

and another Dataframe which has codes mentioned in above dataframne with the descriptions in next column.另一个 Dataframe 具有上述数据帧中提到的代码以及下一列中的描述。

   code           Description
1. D-1799      he is moving now
2. SS-1428     they will have some
3. GTY6QD12    is it well now

I want First Dataframe to be filled with the codes descriptions from 2nd Dataframe based on code.我希望 First Dataframe 根据代码填充 2nd Dataframe 的代码描述。 Required result should be like this.要求的结果应该是这样的。

   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

Use Series.replace by Series from df2 created by DataFrame.set_index and selecting Descriptions column:使用Series.replace by Series from df2 created by 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

If need substring replacement add regex=True :如果需要 substring 替换添加regex=True

df1['Descriptions'] = df1['Descriptions'].replace(s, regex=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM