[英]Pandas - get dataframe rows based on matching columns with other dataframe
假设有两个数据帧,一个叫 df1,另一个叫 df2:
df1 = pd.DataFrame.from_dict({'col1': [1,9,4,7],
'col2': [6,4,3,7],
'col3': [1,4,7,8]})
df2 = pd.DataFrame.from_dict({'col1': [4,8,2,7],
'col2': [7,3,3,3],
'col3': [2,7,7,5]})
df1:
col1 col2 col3
0 1 6 1
1 9 4 4
2 4 3 7
3 7 7 8
df2:
col1 col2 col3
0 4 7 2
1 8 3 7
2 2 3 7
3 7 3 5
如您所见,在 'col2' 和 'col3' 的两个数据帧中,我们都有 (3, 7) 的组合。 我想迭代 df1 的行,并使用 col2 和 col3 作为 df2 的过滤器,这样对于 df1 中的索引(0、1、3),我们将收到一个空的 dataframe,但对于第 2 行,我们将收到一个 dataframe 与 df2 中的索引行 (1,2) 及其原始索引:
new dataframe:
col1 col2 col3
1 8 3 7
2 2 3 7
在这里为任何感兴趣的人找到解决方案: Select 行来自 Pandas DataFrame 与另一个 ZBA834Z5C175EB8E12 中的列值完全相同
df1.merge(df2[['col2', 'col3']])
您可以将pd.merge
与 how as inner
一起使用
df2.reset_index().merge(df1[['col2','col3']], how="inner").set_index('index')
col1 col2 col3
index
1 8 3 7
2 2 3 7
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.