How to replace negative numbers in Pandas Data Frame by zero

前端 未结 5 863
孤独总比滥情好
孤独总比滥情好 2020-11-27 15:09

I would like to know if there is someway of replacing all DataFrame negative numbers by zeros?

5条回答
  •  猫巷女王i
    2020-11-27 15:34

    Another succinct way of doing this is pandas.DataFrame.clip.

    For example:

    import pandas as pd
    
    In [20]: df = pd.DataFrame({'a': [-1, 100, -2]})
    
    In [21]: df
    Out[21]: 
         a
    0   -1
    1  100
    2   -2
    
    In [22]: df.clip(lower=0)
    Out[22]: 
         a
    0    0
    1  100
    2    0
    

    There's also df.clip_lower(0).

提交回复
热议问题