一次更改pandas DataFrame的多个列中的某些值

时间:2021-12-12 22:54:59

Suppose I have the following DataFrame:

假设我有以下DataFrame:

In [1]: df
Out[1]:
  apple banana cherry
0     0      3   good
1     1      4    bad
2     2      5   good

This works as expected:

这按预期工作:

In [2]: df['apple'][df.cherry == 'bad'] = np.nan
In [3]: df
Out[3]:
  apple banana cherry
0     0      3   good
1   NaN      4    bad
2     2      5   good

But this doesn't:

但这不是:

In [2]: df[['apple', 'banana']][df.cherry == 'bad'] = np.nan
In [3]: df
Out[3]:
  apple banana cherry
0     0      3   good
1     1      4    bad
2     2      5   good

Why? How can I achieve the conversion of both the 'apple' and 'banana' values without having to write out two lines, as in

为什么?如何在不必写出两行的情况下实现'apple'和'banana'值的转换,如

In [2]: df['apple'][df.cherry == 'bad'] = np.nan
In [3]: df['banana'][df.cherry == 'bad'] = np.nan

2 个解决方案

#1


32  

You should use loc and do this without chaining:

您应该使用loc并执行此操作而不进行链接:

In [11]: df.loc[df.cherry == 'bad', ['apple', 'banana']] = np.nan

In [12]: df
Out[12]: 
   apple  banana cherry
0      0       3   good
1    NaN     NaN    bad
2      2       5   good

See the docs on returning a view vs a copy, if you chain the assignment is made to the copy (and thrown away) but if you do it in one loc then pandas cleverly realises you want to assign to the original.

请参阅有关返回视图与副本的文档,如果您将链接分配到副本(并丢弃),但如果您在一个位置执行此操作,则pandas会巧妙地意识到您要分配给原始文件。

#2


5  

It's because df[['apple', 'banana']][df.cherry == 'bad'] = np.nan assigning to the copy of DataFrame. Try this:

这是因为df [['apple','banana']] [df.cherry =='bad'] = np.nan分配给DataFrame的副本。尝试这个:

df.ix[df.cherry == 'bad', ['apple', 'banana']] = np.nan

#1


32  

You should use loc and do this without chaining:

您应该使用loc并执行此操作而不进行链接:

In [11]: df.loc[df.cherry == 'bad', ['apple', 'banana']] = np.nan

In [12]: df
Out[12]: 
   apple  banana cherry
0      0       3   good
1    NaN     NaN    bad
2      2       5   good

See the docs on returning a view vs a copy, if you chain the assignment is made to the copy (and thrown away) but if you do it in one loc then pandas cleverly realises you want to assign to the original.

请参阅有关返回视图与副本的文档,如果您将链接分配到副本(并丢弃),但如果您在一个位置执行此操作,则pandas会巧妙地意识到您要分配给原始文件。

#2


5  

It's because df[['apple', 'banana']][df.cherry == 'bad'] = np.nan assigning to the copy of DataFrame. Try this:

这是因为df [['apple','banana']] [df.cherry =='bad'] = np.nan分配给DataFrame的副本。尝试这个:

df.ix[df.cherry == 'bad', ['apple', 'banana']] = np.nan