如果的所有值
id都是唯一的:
我认为你需要
merge用
inner加入。对于
data2仅选择
id列,
on应省略参数,因为在所有列上联接-
仅在此处
id:
df = pd.merge(data1, data2[['id']])
样品:
data1 = pd.Dataframe({'id':list('abcdef'),'B':[4,5,4,5,5,4],'C':[7,8,9,4,2,3]})print (data1) B C id0 4 7 a1 5 8 b2 4 9 c3 5 4 d4 5 2 e5 4 3 fdata2 = pd.Dataframe({'id':list('frcdeg'),'D':[1,3,5,7,1,0],'E':[5,3,6,9,2,4],})print (data2) D E id0 1 5 f1 3 3 r2 5 6 c3 7 9 d4 1 2 e5 0 4 gdf = pd.merge(data1, data2[['id']])print (df) B C id0 4 9 c1 5 4 d2 5 2 e3 4 3 f
如果
id一个或另一个重复
Dataframe使用另一个答案,还添加了类似的解决方案:
df = data1[data1['id'].isin(set(data1['id']) & set(data2['id']))]
ids = set(data1['id']) & set(data2['id'])df = data2.query('id in @ids')
df = data1[np.in1d(data1['id'], np.intersect1d(data1['id'], data2['id']))]
样品:
data1 = pd.Dataframe({'id':list('abcdef'),'B':[4,5,4,5,5,4],'C':[7,8,9,4,2,3]})print (data1) B C id0 4 7 a1 5 8 b2 4 9 c3 5 4 d4 5 2 e5 4 3 fdata2 = pd.Dataframe({'id':list('fecdef'),'D':[1,3,5,7,1,0],'E':[5,3,6,9,2,4],})print (data2) D E id0 1 5 f1 3 3 e2 5 6 c3 7 9 d4 1 2 e5 0 4 fdf = data1[data1['id'].isin(set(data1['id']) & set(data2['id']))]print (df) B C id2 4 9 c3 5 4 d4 5 2 e5 4 3 f
编辑:
您可以使用:
df = data2.loc[data1['id'].isin(set(data1['id']) & set(data2['id'])), ['title']]ids = set(data1['id']) & set(data2['id'])df = data2.query('id in @ids')[['title']]df = data2.loc[np.in1d(data1['id'], np.intersect1d(data1['id'], data2['id'])), ['title']]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)