简体   繁体   English

使用 Pandas 数据框将一列值与另一列中的其他元素列表进行比较

[英]using pandas dataframe compare one column value with other list of elements in another column

using pandas compare two columns and create a new column if both columns match the same string else assign any value from a list of elements使用熊猫比较两列,如果两列匹配相同的字符串,则创建一个新列,否则从元素列表中分配任何值

在此处输入图片说明

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.

相关问题 Pandas 将一列中列表中的项目与另一列中的单个值进行比较 - Pandas compare items in list in one column with single value in another column 按一列中的一个元素列表的值过滤 pandas Dataframe - Filtering pandas Dataframe by value of one list of elements in one column 熊猫将列表元素加入其他数据框列 - pandas join list elements to other dataframe column 如何使用 fuzzywuzzy 比率将一个数据框中的值与另一个数据框中的列进行比较 - How to compare a value in one dataframe to a column in another using fuzzywuzzy ratio pandas 数据框将一个列值附加到另一个具有列表元素的 Pandas 列 - pandas dataframe append a column value to another pandas column which has a list elements 将值列表与 dataframe 列进行比较,如果找到列表中的值,则使用 pandas 比较下一列中的数据 - Compare list of value with dataframe columns and if value in list found compare the data in next column using pandas 遍历一个数据框中的单个列与另一个数据框中的列进行比较使用熊猫在第一个数据框中创建新列 - loop through a single column in one dataframe compare to a column in another dataframe create new column in first dataframe using pandas 将一个 dataframe 中的一列与另一个 dataframe pandas 中的许多列进行比较 - Compare a column in one dataframe with many columns in another dataframe pandas Python将列添加到Pandas Dataframe,这是另一列中的列表元素计数 - Python Add Column to Pandas Dataframe That is a Count of List Elements in Another Column 比较一列中的float值与pandas DataFrame中的所有其他列 - Compare float values in one column with all other columns in a pandas DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM