Put upper and lower bounds on values in DataFrame

2020-05-02 06:16发布

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 with a value >= 1000 the value 1000 in Col 2 and every value <= 100 the value 100.

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

How can I do this?

1条回答
相关推荐>>
2楼-- · 2020-05-02 07:02

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.

查看更多
登录 后发表回答