Home > Net >  Pandas replacing values between a range
Pandas replacing values between a range

Time:06-21

  • Column1(if it is between 0-1023) >>> I will assign a numerical value = 0
  • Column2(if it is between 1024-49151) >>> I will assign a numerical value = 1
  • Column3(if it is between 49152-65535) >>> I will assign a numerical value = 2

I could not understand how to use Python for and & operations.

df.loc[df.Column1 <= 1023, 'Column1'] = 0  

This is what I found

How to assign in this way, please?

  • df.Column1>= 0 && df.Column1 <= 1023 > 0
  • df.Column2>= 1024 && df.Column2 <= 49151 > 1
  • df.Column3>= 49152 && df.Column3 <= 65535 > 2

CodePudding user response:

Enclose the condition in parentheses or use Series.between

df.loc[(df.Column1 >= 0) & (df.Column1 <= 1023) , 'Column1'] = 0
#or
df.loc[df.Column1.between(0, 1023) , 'Column1'] = 0

CodePudding user response:

Use numpy.select with Series.between:

df=pd.DataFrame({"Column1": [0, 1000, 1023, 1024, 65535, 65536]})


m1 = df.Column1.between(0, 1023)
m2 = df.Column1.between(1024, 49151)
m3 = df.Column1.between(49152, 65535)

df['new'] = np.select([m1, m2, m3], [0,1,2], default=None)

Or cut with include_lowest=True parameter:

df['new1']=pd.cut(df.Column1,bins=[0,1023,49151,65535],labels=[0,1,2], include_lowest=True)
print (df)
   Column1   new new1
0        0     0    0
1     1000     0    0
2     1023     0    0
3     1024     1    1
4    65535     2    2
5    65536  None  NaN
  • Related