Put upper and lower bounds on values in DataFrame column

前端 未结 1 1689
梦毁少年i
梦毁少年i 2021-01-23 15:03

I have the following column:

 Column 1
 1
 10
 5
 100
 50
 1000
 50000
 2000
 100
 1000
 3000
 ...

I would like to assign every row in Col1 wit

相关标签:
1条回答
  • 2021-01-23 15:25

    You can use clip:

    >>> df['Column 2'] = df['Column 1'].clip(100, 1000)
    >>> df
        Column 1  Column 2
    0          1       100
    1         10       100
    2          5       100
    3        100       100
    4         50       100
    5       1000      1000
    6      50000      1000
    7       2000      1000
    8        100       100
    9       1000      1000
    10      3000      1000
    

    All values lower than 100 are set to 100; all values greater than 1000 are set to 1000.

    0 讨论(0)
提交回复
热议问题