我有一个 DataFrame,我想用零替换超过某个值的特定列中的值。我曾认为这是实现这一目标的一种方式:
df[df.my_channel > 20000].my_channel = 0
如果我将通道复制到一个新的数据框中,这很简单:
df2 = df.my_channel
df2[df2 > 20000] = 0
这正是我想要的,但似乎不适用于作为原始 DataFrame 一部分的通道。
原文由 BMichell 发布,翻译遵循 CC BY-SA 4.0 许可协议
.ix
索引器适用于 0.20.0 之前的 pandas 版本,但由于 pandas 0.20.0,.ix
索引器已 弃用,因此您应该避免使用它。相反,您可以使用.loc
或iloc
索引器。您可以通过以下方式解决此问题:或者,在一行中,
mask
helps you to select the rows in whichdf.my_channel > 20000
isTrue
, whiledf.loc[mask, column_name] = 0
sets the value 0 to the selected rows wheremask
保存在名称为column_name
的列中。更新: 在这种情况下,你应该使用
loc
因为如果你使用iloc
,你会得到一个NotImplementedError
告诉你 _基于 iLocation 的整数类型的布尔索引不可用_。