为根据熊猫条件选择的行添加特定值

时间:2018-06-29 03:53:35

标签: python pandas

如果我具有以下数据框:

id        fruits
01     Apple, Apricot
02     Apple, Banana, Clementine, Pear
03     Orange, Pineapple, Pear

我想将Fruit添加到Apple所在的行中,以生成这样的新数据框:

id        fruits
01     Apple, Apricot, Fruit
02     Apple, Banana, Clementine, Pear, Fruit
03     Orange, Pineapple, Pear

我应该怎么做?谢谢。对不起,我化妆这个例子代表了我的实际问题。

2 个答案:

答案 0 :(得分:2)

第一个成功的黑客

fruit = np.array(', Fruit', object)
df.fruits + df.fruits.str.contains('Apple') * fruit

0                     Apple, Apricot, Fruit
1    Apple, Banana, Clementine, Pear, Fruit
2                   Orange, Pineapple, Pear
Name: fruits, dtype: object

更合理

df.loc[df.fruits.str.contains('Apple'), 'fruits'] += ', Fruit'
df

   id                                  fruits
0   1                   Apple, Apricot, Fruit
1   2  Apple, Banana, Clementine, Pear, Fruit
2   3                 Orange, Pineapple, Pear

__

要发表评论,在fruits列中的元素不是字符串的地方出现NA。这意味着数据很差。没关系,我们可以填写NA

感谢jezrael改进了实施。

df.loc[df.fruits.str.contains('Apple', na=False), 'fruits'] += ', Fruit'
df

答案 1 :(得分:1)

df['fruits'] = [row + ', Fruit' if 'Apple' in str(row) else row for row in df['fruits']]