更新pandas中满足特定条件的行值 [英] Update row values where certain condition is met in pandas

查看:1145
本文介绍了更新pandas中满足特定条件的行值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有以下数据框:

什么是更新列专长 another_feat 的最有效方法,其中是数字 2

What is the most efficient way to update the values of the columns feat and another_feat where the stream is number 2?

这是吗?

for index, row in df.iterrows():
    if df1.loc[index,'stream'] == 2:
       # do something

更新:
如果我的列数超过100,该怎么办?我不想明确命名我想要更新的列。我想将每列的值除以2(流列除外)。

UPDATE: What to do if I have more than a 100 columns? I don't want to explicitly name the columns that I want to update. I want to divide the value of each column by 2 (except for the stream column).

所以要清楚我的目标是什么:

So to be clear what my goal is:

将所有值除以流2的所有行中的2,但不更改流列

推荐答案

我认为你可以使用 loc 如果您需要将两列更新为相同的值:

I think you can use loc if you need update two columns to same value:

df1.loc[df1['stream'] == 2, ['feat','another_feat']] = 'aaaa'
print df1
   stream        feat another_feat
a       1  some_value   some_value
b       2        aaaa         aaaa
c       2        aaaa         aaaa
d       3  some_value   some_value

如果您需要单独更新,则使用一个选项:

If you need update separate, one option is use:

df1.loc[df1['stream'] == 2, 'feat'] = 10
print df1
   stream        feat another_feat
a       1  some_value   some_value
b       2          10   some_value
c       2          10   some_value
d       3  some_value   some_value

另一个常见的选择是使用 numpy.where

Another common option is use numpy.where:

df1['feat'] = np.where(df1['stream'] == 2, 10,20)
print df1
   stream  feat another_feat
a       1    20   some_value
b       2    10   some_value
c       2    10   some_value
d       3    20   some_value

编辑:如果你需要除法所有列没有其中条件是 True ,使用:

If you need divide all columns without stream where condition is True, use:

print df1
   stream  feat  another_feat
a       1     4             5
b       2     4             5
c       2     2             9
d       3     1             7

#filter columns all without stream
cols = [col for col in df1.columns if col != 'stream']
print cols
['feat', 'another_feat']

df1.loc[df1['stream'] == 2, cols ] = df1 / 2
print df1
   stream  feat  another_feat
a       1   4.0           5.0
b       2   2.0           2.5
c       2   1.0           4.5
d       3   1.0           7.0

这篇关于更新pandas中满足特定条件的行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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