2 回答

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊
正如@QuangHoang 在評(píng)論中所說(shuō),這里的關(guān)鍵是merge. 該indicator=True選項(xiàng)需要一個(gè)額外的_merge列來(lái)指示該行是否存在于其中一個(gè)數(shù)據(jù)幀(以及哪個(gè)數(shù)據(jù)幀)或兩者中:
df1 = pd.DataFrame({'name' : ['tom', 'jerry', 'jennifer', 'hafiz', 'kitty']})
df2 = pd.DataFrame({'name' : ['tom', 'jerry', 'alex', 'hafiz', 'samdin', 'unnar']})
tmp = pd.merge(df1, df2, how='outer', on='name', indicator=True)
good_boy = tmp.loc[tmp['_merge']=='both', 'name'].to_list()
bad_boy = tmp.loc[tmp['_merge']=='left_only', 'name'].to_list()
new_boy = tmp.loc[tmp['_merge']=='right_only', 'name'].to_list()

TA貢獻(xiàn)2036條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以使用DataFrame.join
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.join.html
good_boy = df1.join(df2, on = 'name', how = 'inner')[['name_left']].rename(columns = {'name_left' : 'name'})
bad_boy = df1[~df1['name'].isin(df2['name'].tolist())]
new_boy = df2[~df2['name'].isin(df1['name'].tolist())]
添加回答
舉報(bào)