我如何有条件地更新 pandas 数据框中的多列 [英] How can I conditionally update multiple columns in a panda dataframe

查看:89
本文介绍了我如何有条件地更新 pandas 数据框中的多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试有条件地更新熊猫数据框中的多行.这是我的数据:

I'm trying to conditionally update multiple rows in my panda dataframe. Here's my data:

df = pd.DataFrame([[1,1,1], [2,2,2], [3,3,3]], columns=list('ABC'))

我可以分两步进行所需的更新:

I can do the update I want in two steps:

df.loc[df['A'] == 1, 'B'] = df['C'] +10
df.loc[df['A'] == 1, 'A'] = df['C'] +11

或者我可以一步更新为常量值:

Or I can update to constant values in one step:

df.loc[df['A'] == 1, ['A', 'B']] = [11, 12]

但是我无法在一个步骤中更新其他列中的多个列:

But I can't update multiple columns from other columns in a single step:

df.loc[df['A'] == 1, ['A', 'B']] = [df['C'] + 10, df['C'] + 11]
...
ValueError: shape mismatch: value array of shape (2,3) could not be broadcast to indexing result of shape (1,2)

有什么想法我该怎么做?

Any ideas how I can do this?

感谢@EdChum提供的简单案例的简单解决方案-更新了问题以展示更复杂的现实.

Thanks @EdChum for the simple solution for the simple case - have updated the question to demonstrate a more complex reality.

推荐答案

所以几年后看这个问题,我看到了错误,要强制返回的结果正确分配,您需要访问标量值并使用这些值进行分配,以便它们按需要对齐:

So looking at this question a couple years later I see the error, to coerce the returned result so it assigns correctly you need to access the scalar values and use these to assign so they align as desired:

In [22]:
df.loc[df['A'] == 1, ['A', 'B']] = df['C'].values[0] + 10,df['C'].values[0] + 11
df

Out[22]:
    A   B  C
0  11  12  1
1   2   2  2
2   3   3  3

这篇关于我如何有条件地更新 pandas 数据框中的多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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