Change value with upper and lower limit

Issue

I have one dataset with several column: data-pioggia-name…..

I would like to rename values, within of the column pioggia.
I would like:

  • values<0 = 0
  • values>400 = 0

I tried with:

data.loc[data.pioggia < 0]= "0"

But I have one problem with values > 400 = 0

Solution

To recap, I am guessing your DataFrame is called data and pioggia is the name of the column that serves as filter for the values to be changed.

If so, then the following code should do:

data['pioggia'].loc[(data['pioggia'] < 0) | (data['pioggia'] > 400)] = 0

Note that when you write "0" you are entering a string whose only character is 0, not a number.

Which is the error that you’re facing for values > 400, anyway?

Answered By – Filippo Antonio Capizzi

Answer Checked By – Mildred Charles (AngularFixing Admin)

Leave a Reply

Your email address will not be published.