[英]Compare two columns of one dataframe to one column of another dataframe
I have one dataframe as below:我有一个 dataframe 如下:
Id1![]() |
Id2 ![]() |
---|---|
1 ![]() |
4 ![]() |
2 ![]() |
5 ![]() |
3 ![]() |
The 2nd dataframe is:第2个dataframe是:
ID ![]() |
Comment![]() |
---|---|
1 ![]() |
Pears![]() |
2 ![]() |
Grapes![]() |
3 ![]() |
Orange![]() |
4 ![]() |
Banana![]() |
5 ![]() |
Apple![]() |
How can I get the output like:我怎样才能得到 output 像:
Id1![]() |
Id2 ![]() |
Review![]() |
---|---|---|
1 ![]() |
4 ![]() |
Banana![]() |
2 ![]() |
5 ![]() |
Apple![]() |
3 ![]() |
Orange![]() |
So, basically I am trying to do a look up for Id2 (from dataframe 1) and get the comment from 2nd dataframe but if the Id2 (in first dataframe) is null then get the Id1 comment from 2nd dataframe. So, basically I am trying to do a look up for Id2 (from dataframe 1) and get the comment from 2nd dataframe but if the Id2 (in first dataframe) is null then get the Id1 comment from 2nd dataframe.
Use Series.fillna
for replace missing values in Id2
by Id1
and then mapping column by Series.map
by Series
created by another DataFrame
:使用
Series.fillna
将Id2
中的缺失值替换为Id1
,然后将列映射为Series.map
由另一个DataFrame
创建的Series
:
s = df2.set_index('ID')['Comment']
df1['Comment'] = df1['Id2'].fillna(df1['Id1']).map(s)
If there is multiple ID
columns is possible forward filling missing values and selected last column, then mapping:如果有多个
ID
列可能正向填充缺失值并选择最后一列,则映射:
df1['Comment'] = df1.ffill(axis=1).iloc[:, -1].map(s)
Solution with merge
is possible with helper column:使用辅助列可以使用
merge
解决方案:
df1['ID'] = df1['Id2'].fillna(df1['Id1'])
#another idea
#df1['ID'] = df1.ffill(axis=1).iloc[:, -1]
df = df1.merge(df2, on='ID', how='left')
Or:或者:
df = df1.assign(ID = df1['Id2'].fillna(df1['Id1'])).merge(df2, on='ID', how='left')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.