简体   繁体   English

两个数据框的 Python 交集

[英]Python Intersection of two Dataframe

I need to intersect data frame.我需要相交数据框。 So I executed the following code.所以我执行了以下代码。 But I cannot get the result I want.但我无法得到我想要的结果。

df1 = pd.Dataframe([
      ['psy', 180, 75],
      ['psy', 180, 75]
], columns = ['name', 'height', 'weight'])

df2 = pd.Dataframe([
      ['psy', 180, 75],
      ['df', 160, 65],
      ['ddqq', 170, 75],
      ['psy', 180, 75]
], columns = ['name', 'height', 'weight'])

print(pd.merge(df1, df2, how='inner'))

Output输出

  name  height  weight
0 psy   180     75
1 psy   180     75
2 psy   180     75
3 psy   180     75

But I want the below results.但我想要以下结果。

 name  height  weight
0 psy   180     75
1 psy   180     75

How can I get the following result?我怎样才能得到以下结果? Please Tell me...请告诉我...

You can drop duplicates from one of the DataFrames.您可以从 DataFrame 之一中删除重复项。 For example:例如:

pd.merge(df1, df2.drop_duplicates(), how='inner')

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

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