Python 如何使用pandas从另一个数据帧B的列中删除数据帧A中包含特定数量值的行?
假设我有一个数据帧a,如下所示:Python 如何使用pandas从另一个数据帧B的列中删除数据帧A中包含特定数量值的行?,python,pandas,dictionary,indexing,dataframe,Python,Pandas,Dictionary,Indexing,Dataframe,假设我有一个数据帧a,如下所示: item_name ------------------------- nike power shoes / 50% off / only today nike super t-shirt / adidas / cool sale reebok power t-shirt / reebock shoes 另一个数据框B是一个品牌词典,它是: (假设锐步和锐步是“锐步”的一些变体) 我的问题是我想从数据帧A中删除行 基于数据框B,包含超过1个品牌的。 因
item_name
-------------------------
nike power shoes / 50% off / only today
nike super t-shirt / adidas / cool sale
reebok power t-shirt / reebock shoes
另一个数据框B是一个品牌词典,它是:
(假设锐步和锐步是“锐步”的一些变体) 我的问题是我想从数据帧A中删除行 基于数据框B,包含超过1个品牌的。 因此,我期望的结果如下:
item_name
nike power shoes / 50% off / only today
reebok power t-shirt / reebock shoes
你可以看到只有“nike super t恤/阿迪达斯/酷炫销售”一行
已被删除,因为我们有[耐克]和[阿迪达斯]。
重要的是要知道,这种逻辑是基于原始品牌,而不是变异部分。
因此,“锐步动力t恤/锐步鞋”一行并未删除,因为锐步和锐步属于一个独特的品牌名称[锐步]
我如何通过熊猫完成这项工作?(不使用列表理解)
请给我帮助!:) 您可以与遮罩一起使用。它是通过使用lambda函数创建的。首先将所有单词转换为系列
,然后通过d
并比较值:
item_name
nike power shoes / 50% off / only today
reebok power t-shirt / reebock shoes
d = df2.set_index('variation')['original']
mask = df1.item_name.apply(lambda x: pd.Series(x.split()).map(d).nunique() > 1)
print (mask)
0 False
1 True
2 False
Name: item_name, dtype: bool
print (df1[~mask])
item_name
0 nike power shoes / 50% off / only today
2 reebok power t-shirt / reebock shoes