问题描述
我正在处理熊猫中的两个数据框:
I'm working with two dataframes in pandas:
DF1:Product_ID,Num_Reviews
DF1: Product_ID, Num_Reviews
DF2:Product_ID,Reviewer_ID,Review_Score
DF2: Product_ID, Reviewer_ID, Review_Score
我想删除或过滤DF2以仅包含DF1中存在具有Product_ID的条目.在这件事上,我对熊猫甚至python都不是很熟悉,并且找不到一种明确的方法来检查数据框是否包含键和基于该键的过滤器.
I want to remove or filter DF2 to only contain entries with a Product_ID that exists in DF1. I'm not very familiar with pandas or even python for that matter, and couldn't find a clear way to check if a dataframe includes a key and filter based on that.
谢谢!
推荐答案
正在执行此操作.
df2[df2['Product_ID'].isin(df1['Product_ID'].unique())]
从df1
获取唯一的Product_ID
,并使用isin()
Get unique Product_ID
from df1
and filter those values in df2['Product_ID']
using isin()
这篇关于Python Pandas-删除一个数据框中所有未包含在其他数据框中的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!