[英]In Pandas how can I test whether a value exists in two dataframes given a unique ID?
I have two dataframes like this where both frames can have multiple entries for each ID:我有两个这样的数据帧,其中两个帧都可以为每个 ID 包含多个条目:
The first dataframe df1:第一个 dataframe df1:
ID ID | Amount数量 |
---|---|
101 101 | $30 30 美元 |
102 102 | $300 300 美元 |
101 101 | $35 35 美元 |
103 103 | $10 10 美元 |
104 104 | $220 220 美元 |
103 103 | $370 370 美元 |
101 101 | $500 500 美元 |
105 105 | $65 65 美元 |
The second dataframe df2 has fewer rows but the same kind of information:第二个 dataframe df2 的行数较少但信息类型相同:
ID ID | Amount数量 |
---|---|
101 101 | $35 35 美元 |
102 102 | $2 2美元 |
103 103 | $25 25 美元 |
104 104 | $75 75 美元 |
101 101 | $30 30 美元 |
102 102 | $900 900 美元 |
I want to compare the dataframes and find any rows that have the same Amount for a given ID in both frames.我想比较数据帧并在两个帧中找到对于给定 ID 具有相同数量的任何行。 Here, for example, I want pandas to return ID 101 has having an entry for $30 in both frames.例如,在这里,我希望 pandas 返回 ID 101 在两个框架中都有 30 美元的条目。 Right now my intuition is to append the dataframes like so:现在我的直觉是 append 数据帧如下:
df1 = df1.append(df2,ignore_index=True)
df1[df1.duplicated()]
But I wanted to know if there's a more 'pythonic' way to do this.但我想知道是否有更“pythonic”的方式来做到这一点。 Thanks!谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.