簡體   English   中英

檢查 pandas 列值是否在另一列的列表中

[英]Check if pandas column value is inside another column's list

我有一個像這樣的 pandas 列,其中 amount 是一個字符串列:

id      amount    possible_amount
0        1.00       ['1.00', '2.00', '3.00']
1       45.00       ['100.00', '45.00']
2       37.00       ['29.00', '38.00']

我想創建一個名為“match”的新列,如果amountpossible_amount列表中,則其值為True ,否則為False 所以上面例子的預期結果是:

id      amount    possible_amount                     match
0        1.00       ['1.00', '2.00', '3.00']           True
1       45.00       ['100.00', '45.00']                True
2       37.00       ['29.00', '38.00']                 False

我嘗試了幾種不同的方法,以下是其中之一。 還嘗試使用str.contains()無濟於事。

df['match'] = np.where(df['amount'].isin(df['possible_amount']), True, False)

但這只會返回match的所有 False 。

將值轉換為浮點數並在列表理解中進行比較:

df['match'] = [a in list(map(float, b)) for a, b in zip(df['amount'],df['possible_amount'])]
print (df)
   id  amount     possible_amount  match
0   0     1.0  [1.00, 2.00, 3.00]   True
1   1    45.0     [100.00, 45.00]   True
2   2    37.0      [29.00, 38.00]  False

另一個想法,顯然更慢:

df['match'] = (df.explode('possible_amount')
                 .assign(possible_amount = lambda x: x['possible_amount'].astype(float),
                         new = lambda x: x['possible_amount'].eq(x['amount']))
                 .groupby(level=0)['new']
                 .any()
         )

print (df)
   id  amount     possible_amount  match
0   0     1.0  [1.00, 2.00, 3.00]   True
1   1    45.0     [100.00, 45.00]   True
2   2    37.0      [29.00, 38.00]  False

暫無
暫無

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

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