Conditional Replace Pandas

后端 未结 6 762
灰色年华
灰色年华 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:29

    The reason your original dataframe does not update is because chained indexing may cause you to modify a copy rather than a view of your dataframe. The docs give this advice:

    When setting values in a pandas object, care must be taken to avoid what is called chained indexing.

    You have a few alternatives:-

    loc + Boolean indexing

    loc may be used for setting values and supports Boolean masks:

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

    mask + Boolean indexing

    You can assign to your series:

    df['my_channel'] = df['my_channel'].mask(df['my_channel'] > 20000, 0)
    

    Or you can update your series in place:

    df['my_channel'].mask(df['my_channel'] > 20000, 0, inplace=True)
    

    np.where + Boolean indexing

    You can use NumPy by assigning your original series when your condition is not satisfied; however, the first two solutions are cleaner since they explicitly change only specified values.

    df['my_channel'] = np.where(df['my_channel'] > 20000, 0, df['my_channel'])
    

提交回复
热议问题