Conditional Replace Pandas

后端 未结 6 841
灰色年华
灰色年华 2020-11-21 10:17

I have a DataFrame, and I want to replace the values in a particular column that exceed a value with zero. I had thought this was a way of achieving this:

df[         


        
6条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-21 10:45

    .ix indexer works okay for pandas version prior to 0.20.0, but since pandas 0.20.0, the .ix indexer is deprecated, so you should avoid using it. Instead, you can use .loc or iloc indexers. You can solve this problem by:

    mask = df.my_channel > 20000
    column_name = 'my_channel'
    df.loc[mask, column_name] = 0
    

    Or, in one line,

    df.loc[df.my_channel > 20000, 'my_channel'] = 0
    

    mask helps you to select the rows in which df.my_channel > 20000 is True, while df.loc[mask, column_name] = 0 sets the value 0 to the selected rows where maskholds in the column which name is column_name.

    Update: In this case, you should use loc because if you use iloc, you will get a NotImplementedError telling you that iLocation based boolean indexing on an integer type is not available.

提交回复
热议问题