Set maximum value (upper bound) in pandas DataFrame

后端 未结 3 1569
温柔的废话
温柔的废话 2020-12-02 01:50

I\'m trying to set a maximum value of a pandas DataFrame column. For example:

my_dict = {\'a\':[10,12,15,17,19,20]}
df = pd.DataFrame(my_dict)

df[\'a\'].se         


        
相关标签:
3条回答
  • 2020-12-02 02:13

    I suppose you can do:

    maxVal = 15
    df['a'].where(df['a'] <= maxVal, maxVal)      # where replace values with other when the 
                                                  # condition is not satisfied
    
    #0    10
    #1    12
    #2    15
    #3    15
    #4    15
    #5    15
    #Name: a, dtype: int64
    

    Or:

    df['a'][df['a'] >= maxVal] = maxVal
    
    0 讨论(0)
  • 2020-12-02 02:15

    numpy.clip is a good, fast alternative.

    df
    
        a
    0  10
    1  12
    2  15
    3  17
    4  19
    5  20
    
    np.clip(df['a'], a_max=15, a_min=None)
    
    0    10
    1    12
    2    15
    3    15
    4    15
    5    15
    Name: a, dtype: int64
    
    # Or,
    np.clip(df['a'].to_numpy(), a_max=15, a_min=None)
    # array([10, 12, 15, 15, 15, 15])
    

    From v0.21 onwards, you can also use DataFrame.clip_upper.

    Note
    This method (along with clip_lower) has been deprecated from v0.24 and will be removed in a future version.

    df.clip_upper(15)
    # Or, for a specific column,
    df['a'].clip_upper(15)
    
        a
    0  10
    1  12
    2  15
    3  15
    4  15
    5  15
    

    In similar vein, if you only want to set the lower bound, use DataFrame.clip_lower. These methods are also avaliable on Series objects.

    0 讨论(0)
  • 2020-12-02 02:23

    You can use clip.

    Apply to all columns of the data frame:

    df.clip(upper=15)
    

    Otherwise apply to selected columns as seen here:

    df.clip(upper=pd.Series({'a': 15}), axis=1)
    
    0 讨论(0)
提交回复
热议问题