简体   繁体   English

根据来自另一个数据框的行中的匹配值排除数据框中的行

[英]Exclude rows in a dataframe based on matching values in rows from another dataframe

I have two dataframes (A and B). 我有两个数据框(A和B)。 I want to remove all the rows in B where the values for columns Month, Year, Type, Name are an exact match. 我想删除B中所有行,其中月份,年份,类型,名称列的值完全匹配。

Dataframe A 数据框A

   Name    Type   Month   Year  country Amount   Expiration  Paid
0 EXTRON   GOLD   March   2019    CA    20000   2019-09-07   yes
0 LEAF    SILVER  March   2019    PL    4893    2019-02-02   yes       
0 JMC     GOLD    March   2019    IN    7000    2020-01-16   no       

Dataframe B 数据框B

  Name     Type   Month   Year  country Amount   Expiration  Paid
0 JONS    GOLD    March   2018    PL    500     2019-10-17   yes
0 ABBY    BRONZE  March   2019    AU    60000   2019-02-02   yes       
0 BUYT     GOLD   March   2018    BR     50     2018-03-22   no       
0 EXTRON  GOLD    March   2019    CA    90000   2019-09-07   yes
0 JAYB    PURPLE  March   2019    PL    9.90    2018-04-20   yes       
0 JMC     GOLD    March   2019    IN    6000    2020-01-16   no       
0 JMC     GOLD    April   2019    IN    1000    2020-01-16   no      

Desired Output: 所需输出:

Dataframe B 数据框B

  Name       Type   Month   Year  country Amount   Expiration  Paid
0 JONS    GOLD    March   2018    PL    500     2019-10-17   yes
0 ABBY    BRONZE  March   2019    AU    60000   2019-02-02   yes       
0 BUYT     GOLD   March   2018    BR     50     2018-03-22   no       
0 JAYB    PURPLE  March   2019    PL    9.90    2018-04-20   yes       
0 JMC     GOLD    April   2019    IN    1000    2020-01-16   no

We can using merge here 我们可以在这里使用merge

l=['Month', 'Year','Type', 'Name']
B=B.merge(A[l],on=l,indicator=True,how='outer').loc[lambda x : x['_merge']=='left_only'].copy() 
# you can add drop here like B=B.drop('_merge',1)
   Name    Type  Month  Year country   Amount  Expiration Paid     _merge
0  JONS    GOLD  March  2018      PL    500.0  2019-10-17  yes  left_only
1  ABBY  BRONZE  March  2019      AU  60000.0  2019-02-02  yes  left_only
2  BUYT    GOLD  March  2018      BR     50.0  2018-03-22   no  left_only
4  JAYB  PURPLE  March  2019      PL      9.9  2018-04-20  yes  left_only
6   JMC    GOLD  April  2019      IN   1000.0  2020-01-16   no  left_only

I tried using MultiIndex for the same. 我尝试使用相同的MultiIndex

cols =['Month', 'Year','Type', 'Name']
index1 = pd.MultiIndex.from_arrays([df1[col] for col in cols])
index2 = pd.MultiIndex.from_arrays([df2[col] for col in cols])
df2 = df2.loc[~index2.isin(index1)]

暂无
暂无

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

相关问题 根据另一个 DataFrame 中的匹配行更新 DataFrame - Update DataFrame based on matching rows in another DataFrame 根据另一个 dataframe 中的值删除 dataframe 中的行 - Dropping rows in dataframe based on values in another dataframe 根据另一个数据框的值在数据框中添加行 - Adding rows in dataframe based on values of another dataframe Python DataFrame - 根据另一个数据帧中的值选择数据帧行 - Python DataFrame - Select dataframe rows based on values in another dataframe 根据另一个数据框中的值从DataFrame中选择行,并根据第二个DataFrame使用值更新其中一个列 - Select rows from a DataFrame based on a values in another dataframe and updating one of the column with values according to the second DataFrame 一个 dataframe 中的值基于另一个行中的条件 - Looing values in one dataframe based on conditions from rows in another Pandas 根据另一个数据框中 2 列的值过滤行 - Pandas filter rows based on values from 2 columns in another dataframe 根据另一个数据帧行更新数据帧行 - Updating a dataframe rows based on another dataframe rows 列表理解从包含另一个数据帧的匹配列值的数据帧中获取行 - List comprehension to get rows from a dataframe that contains matching column values of another dataframe 将多个字符串从一个数据帧中的行匹配到另一个数据帧中的行 - Matching multiple strings from rows in one dataframe to rows in another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM