简体   繁体   English

如何通过 python 中不同大小的其他 dataframe 列中的值过滤 dataframe?

[英]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.

相关问题 Python:过滤 dataframe 行,以便值位于其他 dataframe 的列中 - Python: filter dataframe rows so that values are in columns of other dataframe python pandas 如何将列乘以不同 dataframe 中的其他值 - python pandas how to multiplication columns by other values in different dataframe dataframe 通过键分配不同大小的其他 dataframe 的列 - dataframe assign columns from other dataframe with different size by key 如何用其他数据框中的 id 替换数据框中的 2 列值? - How to substitute 2 columns values in dataframe with their ids from other dataframe? 过滤并计算python中包含不同列中的值的数据框的长度 - filter and compute the length of a dataframe in python which contains values in different columns Python数据框-来自3列的数据值将映射到其他3列下 - Python dataframe - data values from 3 columns to be mapped under other 3 columns Python DataFrame:将DataFrame中的值替换为具有相同索引和列的其他DataFrame - Python DataFrame: replacing values from DataFrame to other DataFrame with same index and columns Pandas:用于匹配行索引 - 使用不同列大小的其他 dataframe 的值更新 dataframe 值 - Pandas: for matching row indices - update dataframe values with values from other dataframe with a different column size 使用 python 中其他 dataframe 的多列过滤一个 dataframe - Filter of one dataframe using multiple columns of other dataframe in python Python数据框使用其他列中的信息填充NaN值 - Python Dataframe filling NaN values using information from other columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM