[英]How to filter dataframe by values from columns in other dataframe thats different size in python?
Hey i am trying to filter dataframe df2 by values in dataframe df1 in columns 'Currency' and 'Type'.嘿,我正在尝试通过“货币”和“类型”列中 dataframe df1 中的值过滤 dataframe df2。
df1: df1:
Currency Type Amount
USD Buy 13003,00
EUR Sell 920,00
df2: df2:
Currency Type Amount
USD Buy 21414,00
USD Sell 56236,00
USD Sell 15151,00
PLN Buy 1235,00
EUR Sell 951,00
EUR Buy 1451,00
EUR Buy 961,00
I want to filter df2 so it will drop rows that have the same Currency and opposite Type in df1.我想过滤 df2,以便它会删除 df1 中具有相同货币和相反类型的行。 I am looking to get result like this:我希望得到这样的结果:
Currency Type Amount
USD Sell 56236,00
USD Sell 15151,00
EUR Buy 1451,00
EUR Buy 961,00
One way to do it (though I'm not sure if it's the easiest):一种方法(虽然我不确定这是否是最简单的):
df3 = pd.merge(df2, df1[['Currency', 'Type']], how='left', on=['Currency']).dropna()
df3 = df3[df3['Type_x'] != df3['Type_y']]
del df3['Type_y']
And in the end you can rename the Column 'Type_x' if you need to.最后,如果需要,您可以重命名列“Type_x”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.