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

Pandas add noise to specific column

This is my table below, I want to use pandas to add some noise to x column only, my current code does not work.

x     y
-------
30    0
1     1
0     1
300   0
....

I want only add noise to y==1
noise = np.random.normal(50, 10, ???)

result

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

x(float)            y
----------------
30          0
1+noise     1
0+noise     1
300         0
....

>Solution :

Use DataFrame.loc with count nuber of values by sum:

m = df.y==1 
df.loc[m, 'x'] += np.random.normal(50, 10, m.sum())
print (df)
            x  y
0   30.000000  0
1   52.623817  1
2   56.042890  1
3  300.000000  0

Or generate array same length like length of DataFrame – then use Series.mask or numpy.where:

df['x'] = df['x'].mask(df.y==1, np.random.normal(50, 10, len(df)))
#alternative
#df['x'] = np.where(df.y==1, np.random.normal(50, 10, len(df)), df['x'])
print (df)
            x  y
0   30.000000  0
1   37.968245  1
2   46.963821  1
3  300.000000  0
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