Juan Chô
Juan Chô

Reputation: 572

Replacing values in pandas data frame

I am looking for a pythonic way of replacing values based on whether values are big of small. Say I have a data frame:

ds = pandas.DataFrame({'x' : [4,3,2,1,5], 'y' : [4,5,6,7,8]})

I'd like to replace values on x which are lower than 2 by 2 and values higher than 4 by 4. And similarly with y values, replacing values lower than 5 by 5 and values higher than 7 by 7 so as to get this data frame:

ds = pandas.DataFrame({'x' : [4,3,2,2,4], 'y' : [5,5,6,7,7]})

I did it by iterating on the rows but is really ugly, any more pandas-pythonic way (Basically I want to eliminate extreme values)

Upvotes: 1

Views: 49

Answers (2)

Karn Kumar
Karn Kumar

Reputation: 8816

One way of doing this as follows:

>>> ds[ds.x.le(2) ] =2
>>> ds[ds.x.ge(4) ] =4
>>> ds
   x  y
0  4  4
1  3  5
2  2  6
3  2  2
4  4  4

Upvotes: 1

BENY
BENY

Reputation: 323226

You can check with clip

ds.x.clip(2,4)
Out[42]: 
0    4
1    3
2    2
3    2
4    4
Name: x, dtype: int64
#ds.x=ds.x.clip(2,4)
#ds.y=ds.y.clip(5,7)

Upvotes: 1

Related Questions