假设我有以下的DataFrame:
In [1]: df
Out[1]:
apple banana cherry
0 0 3 good
1 1 4 bad
2 2 5 good
这个按预期工作:
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
但这不会:
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
为什么?我怎样才能在不写两行代码的情况下同时转换“apple”和“banana”值呢?
In [2]: df['apple'][df.cherry == 'bad'] = np.nan
In [3]: df['banana'][df.cherry == 'bad'] = np.nan
df[['apple', 'banana']][df.cherry == 'bad']
相同的数据框架是否容易扩展? - dermenother_df.values
。如果这种方法失败的话,也许值得提出另一个问题? - Andy Haydenother_df.values
赋值是正确的方法。 - dermen