簡體   English   中英

從其他 dataframe 的條件向 pandas dataframe 添加真/假值

[英]Adding True / False values to a pandas dataframe from a condition on other dataframe

我有兩個數據框:

a = pd.DataFrame({'id': [10, 20, 30, 40, 50, 60, 70]})
b = pd.DataFrame({'id': [10, 30, 40, 70]})

print(a)
print(b)

# a
   id
0  10
1  20
2  30
3  40
4  50
5  60
6  70

# b
   id
0  10
1  30
2  40
3  70

如果id出現在b上,我試圖在a中有一個額外的列,如下所示:

# a
   id   present
0  10   True
1  20   False
2  30   True
3  40   True
4  50   False
5  60   False
6  70   True

我試過的:

a.join(b,rsuffix='a')
# and then thought I'd replace nans with False and values with True
# but it does not return what I expect as it joined row by row

    id  ida
0   10  10.000
1   20  30.000
2   30  40.000
3   40  70.000
4   50  nan
5   60  nan
6   70  nan

然后我補充說:

a.join(b,rsuffix='a', on='id')

但也沒有得到我的預期:

    id  ida
0   10  nan
1   20  nan
2   30  nan
3   40  nan
4   50  nan
5   60  nan
6   70  nan

我也嘗試a['present'] = b['id'].isin(a['id'])但這不是我所期望的:

id  present
0   10  True
1   20  True
2   30  True
3   40  True
4   50  NaN
5   60  NaN
6   70  NaN

如何在a中有一個額外的列來表示 b 中是否存在帶有 True / False 語句的id

你很接近,需要在Series.isin中用b['id']測試a['id']

a['present'] = a['id'].isin(b['id'])
print (a)
   id  present
0  10     True
1  20    False
2  30     True
3  40     True
4  50    False
5  60    False
6  70     True

可以在左連接和測試_merge列中both參數indicator=True進行merge

a['present'] = a.merge(b, on='id', how='left', indicator=True)['_merge'].eq('both')
print (a)
   id  present
0  10     True
1  20    False
2  30     True
3  40     True
4  50    False
5  60    False
6  70     True

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM