Mary A
Mary A

Reputation: 23

How do I replace values in an entire dataset?

So I need to replace all values in my dataframe named 'x', that are greater than 140 and less than 35, with 0. I have 600 columns all named 1-600. I've tried the method below but I can't figure out how I can apply this to the entire dataset.

x=x.mask(x['1']>140,0)

When I try doing this

x=x.mask(x>140,0)

This is the error message:

TypeError: '>' not supported between instances of 'str' and 'int'

Can someone please help me out, thank you.

Upvotes: 2

Views: 362

Answers (2)

Narendra Prasath
Narendra Prasath

Reputation: 1531

First convert your dataframe all columns to int type using df = df.astype(int)

You could try numpy astype:

a = df.values
#convert datatype to int
a = a.astype(int)
pd.DataFrame(np.where(( a > 140) | (a < 35) , 0, a))

Also, you can use mask as follows:

df.mask((df > 140) | (df < 35) , 0)

Upvotes: 2

fcdt
fcdt

Reputation: 2493

If you write x, it means the dataframe, in order to address the content of it, you have to write x[:]:

x = x.mask(x[:]>140, 0)

Upvotes: 2

Related Questions