Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python changing value of column with loc doesnt change nothing

I have df that I want to change the column ‘b’ for all rows when column a is equal to 1, to numbers in the range [0.5,1].
for example:

a   b
0   0.2
0   0.4
1   0.02
1   0.001

desire df:

a   b
0   0.2
0   0.4
1   0.7
1   0.8

My code is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df[df['a']==1].reset_index(drop=True).loc[0:len(df[df['a']==1]), 'b'] = np.linspace(0.5,0.99,len (df[df['a']==1]))

But nothing changed.
Thx

>Solution :

IIUC use:

m = df['a']==1
df.loc[m, 'b'] = np.linspace(0.5,0.99, m.sum())
print (df)
   a     b
0  0  0.20
1  0  0.40
2  1  0.50
3  1  0.99
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading