pandas :搜索值并将其添加到多列单元格中 [英] Pandas: search and add values to cells of multiple column

查看:70
本文介绍了 pandas :搜索值并将其添加到多列单元格中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我搜索像这样的数据框df[df['a'] > 5],然后我想分别将5和10分别添加到搜索结果的列bc中.

Suppose I search a dataframe like this df[df['a'] > 5] and then I want to add 5 and 10 respectively to column b and c of the search result.

例如,

 a  b  c
 7  2  5
 3  4  7
 6  1  3

现在我要将5和10加到a> 5的单元格中,结果应如下所示

now I want to add 5 and 10 to cells where a>5 and the result should look like this

 a  b  c
 7  7  15
 3  4  7
 6  6  13

那么,我该怎么做?

推荐答案

一线就能解决问题:

df.loc[df.a > 5, ['b', 'c']] += (5, 10)

这篇关于 pandas :搜索值并将其添加到多列单元格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆