Home > Software design >  Randomly Modify Entries in CSV using Pandas
Randomly Modify Entries in CSV using Pandas

Time:11-03

I have a CSV file containing data like this:

DateTime, product_x, product_y, product_z
2018-01-02 00:00:00,945,1318,17.12
2018-01-03 00:00:00,958,1322,17.25
...

I want to use Python and Pandas to modify the values for product_x, product_y and product_z by some random amount - say adding a random value from -3 - 3 to each, and then writing the result back to a CSV.

EDIT: I need each cell shifted by a different amount (except for random coincidences).

How do I do this please?

CodePudding user response:

Use np.random.randint with columns names in list for generate 2d array and add to original columns filtered in same list:

cols = ['product_x','product_y','product_y']

#dynamic columns names
#cols = df.filter(like='product').columns
df[cols]  = np.random.randint(-3, 3, size=(len(df.index), len(cols)))

print (df)
              DateTime  product_x  product_y  product_z
0  2018-01-02 00:00:00        947       1320      17.12
1  2018-01-03 00:00:00        958       1323      17.25
  • Related