将大于x的值设置为x,将小于-x的值设置为-x

时间:2018-06-01 03:04:18

标签: python pandas random

我已经尝试过,但我无法弄明白这一点。我必须在下面的数据框中更改大于3到3且小于-3到-3的所有数据。

np.random.seed(42)
randomdata = DataFrame(np.random.randn(400, 4))

我已尝试过循环,.loc,.where,似乎没有任何工作。

2 个答案:

答案 0 :(得分:1)

编辑:对于熊猫,熊猫有DataFrame.clip

请参阅numpy.clip.

import numpy as np

thing = np.arange(-5, 6)  # [-5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5]

clipped_thing = np.clip(thing, -3, 3)

print(clipped_thing)

输出

[-3 -3 -3 -2 -1  0  1  2  3  3  3]

答案 1 :(得分:1)

# Creating a data frame with 4 variables and 400 observations
np.random.seed(42)
randomdata = pd.DataFrame(np.random.randn(400, 4))
randomdata.head()

        0           1           2          3
0    0.496714   -0.138264    0.647689    1.523030
1   -0.234153   -0.234137    1.579213    0.767435
2   -0.469474    0.542560   -0.463418   -0.465730
3    0.241962   -1.913280   -1.724918   -0.562288
4   -1.012831    0.314247   -0.908024   -1.412304

# Cap and floor for one variable
randomdata[0].clip(lower=-0.5, upper=0.5)

# Cap and floor entire dataframe
clean_df = randomdata.clip(lower=-0.5, upper=0.5)
clean_df.head()

       0            1           2          3
0    0.496714   -0.138264    0.500000    0.50000
1   -0.234153   -0.234137    0.500000    0.50000
2   -0.469474    0.500000   -0.463418   -0.46573
3    0.241962   -0.500000   -0.500000   -0.50000
4   -0.500000    0.314247   -0.500000   -0.50000