how to clip pandas dataframe column-wise?

后端 未结 2 1254
一个人的身影
一个人的身影 2021-01-05 03:38

I have

In [67]: a
Out[67]:

   0  1  2
0  1  2  3
1  4  5  6

when I run

In [69]: a.clip(lower=[1.5,2.5,3.5],axis=1)
         


        
2条回答
  •  有刺的猬
    2021-01-05 04:31

    Instead of a numpy array, you can use a Series so the labels are aligned:

    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
    

提交回复
热议问题