Conditional Replace Pandas

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

    I would use lambda function on a Series of a DataFrame like this:

    f = lambda x: 0 if x>100 else 1
    df['my_column'] = df['my_column'].map(f)
    

    I do not assert that this is an efficient way, but it works fine.

提交回复
热议问题