2 回答

TA貢獻(xiàn)1874條經(jīng)驗(yàn) 獲得超12個(gè)贊
只有你需要:
如果 ID 是索引,則將 ID 作為列:
df1.reset_index(inplace=True) #if ID is the index
df2.reset_index(inplace=True) #if ID is the index
filtered_df1=df1[df1['var1'].eq('Foo')]
print(filtered_df1)
ID var1
0 1 Foo
1 2 Foo
2 3 Foo
df2.loc[df2['ID'].isin(filtered_df1['ID'])]
ID var1
0 2 Foo
1 3 Bar

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊
df1 = pd.DataFrame({"id":[1,2,3,4], "var1":['Foo', 'Foo', 'Foo', 'Bar']})
df2 = pd.DataFrame({"id":[2,3,4,5, 6], "var1":['Foo','Bar','Bar','Foo', 'Bar']})
def filter(x):
return x == 'Foo'
df1[df1['var1'].apply(filter)]
id var1
0 1 Foo
1 2 Foo
2 3 Foo
df2[df2['var1'].apply(filter)]
id var1
0 2 Foo
3 5 Foo
添加回答
舉報(bào)