Home > OS >  Setting the minimum value of a pandas column using clip
Setting the minimum value of a pandas column using clip

Time:05-22

I want to set the minimum value of a column of pandas dataframe using clip method. Below is my code

import pandas as pd
data = pd.DataFrame({'date' : pd.to_datetime(['2010-12-31', '2012-12-31', '2012-12-31']), 'val' : [1,2, 5]})
data.clip(lower=pd.Series({'val': 4}), axis=1)

Above code is giving error. Could you please help on how to eliminate the error?

CodePudding user response:

You can try Series.clip or set the date column as index then DataFrame.clip.

data['val'] = data['val'].clip(4)

# or

data = (data.set_index('date')
        .clip(4)
        .reset_index())
print(data)

        date  val
0 2010-12-31    4
1 2012-12-31    4
2 2012-12-31    5
  • Related