如何按列方式剪辑pandas dataframe?

bil*_*ill 7 python pandas

我有

In [67]: a
Out[67]:

   0  1  2
0  1  2  3
1  4  5  6
Run Code Online (Sandbox Code Playgroud)

我跑的时候

In [69]: a.clip(lower=[1.5,2.5,3.5],axis=1)
Run Code Online (Sandbox Code Playgroud)

我有

ValueError: other must be the same shape as self when an ndarray
Run Code Online (Sandbox Code Playgroud)

这是预期的吗?我期待得到类似的东西:

Out[72]:

     0    1    2
0  1.5  2.5  3.5
1  4.0  5.0  6.0
Run Code Online (Sandbox Code Playgroud)

ayh*_*han 7

您可以使用系列而不是numpy数组,以便标签对齐:

df
Out: 
   A  B
0  1  4
1  2  5
2  3  6

df.clip(lower=pd.Series({'A': 2.5, 'B': 4.5}), axis=1)
Out: 
     A    B
0  2.5  4.5
1  2.5  5.0
2  3.0  6.0
Run Code Online (Sandbox Code Playgroud)