![](/img/trans.png)
[英]Pandas compare items in list in one column with single value in another column
[英]using pandas dataframe compare one column value with other list of elements in another column
檢查single fruit
是否在multiple fruits
然后返回single fruit
否則在multiple fruits
隨機選擇一個multiple fruits
:
import numpy as np
df['output'] = df.apply(lambda x: x['single fruit']
if x['single fruit'] in x['multiple fruits']
else np.random.choice(x['multiple fruits']), axis=1)
輸出:
>>> df
single fruit multiple fruits output
0 apple [apple, mango] apple
1 grapes [grapes] grapes
2 strawberry [strawberry, grapes, mango] strawberry
3 pineapple [apple, mango] apple
4 graps [strawberry, mango] strawberry
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.