[英]using pandas dataframe compare one column value with other list of elements in another column
Check if single fruit
is in multiple fruits
then return single fruit
else choose a random fruit in multiple fruits
:检查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)
Output:输出:
>>> 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.