Update row values where certain condition is met in pandas(更新 pandas 中满足特定条件的行值)
问题描述
假设我有以下数据框:
更新 feat 和 another_feat 列的值的最有效方法是什么/strong>?
What is the most efficient way to update the values of the columns feat and another_feat where the stream is number 2?
是这个吗?
更新:如果我有超过 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:
如果您需要单独更新,一个选项是使用:
If you need update separate, one option is use:
另一个常见的选项是使用 numpy.where
:
Another common option is use numpy.where
:
如果您需要在条件为 True
的情况下划分所有不带 stream
的列,请使用:
If you need divide all columns without stream
where condition is True
, use:
如果可以使用多个条件,请使用多个 numpy.在哪里
或 numpy.select
:
If working with multiple conditions is possible use multiple numpy.where
or numpy.select
:
这篇关于更新 pandas 中满足特定条件的行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!