简体   繁体   中英

How to add a randomly generated number to elements in a pandas DataFrame

I am trying to add/subtract a random number from existing elements (floats) in a pandas DataFrame (Python).

indices is a random subset index, and modify_columns is a list of the columns I wish to modify. My DataFrame is as follows ( active_set.loc[indices,modify_columns] ):

    Values
380977  0.0
683042  0.0
234012  0.0
16517   0.0
... ...

I would like to add or subtract a randomly generated integer (either -1 or 1) from these values.

I have tried using (2*np.random.randint(0,2,size=(count))-1) to generate an array of these random numbers, and add them:

active_set.loc[indices,modify_columns] = active_set.loc[indices,modify_columns] + (2*np.random.randint(0,2,size=(count))-1)

This does not work as there is a ValueError: Unable to coerce to Series, length must be 1: given 180 . I think I can simply create a second DataFrame with the random numbers, or iterate, but these seem inefficient, and there must be a way to use .apply , so I am asking for some help on how to do this.

more general

df.loc[indexes,columns] = df.loc[indexes,columns] + 2*np.random.randint(0,50,size=(len(indexes),len(columns)))

if you want to add different random values, you can make your random.randint the same size as columns

Create array by same size like length of indices by parameter size for 2d array:

arr = 2*np.random.randint(0,2,size=(len(indices), len(modify_columns)))
active_set.loc[indices,modify_columns] += arr

The easiest solution is to use pandas.DataFrame.add function.

vector_to_add = 2*np.random.randint(0, 2, size=(count)) - 1
df.loc[indices, modify_columns] = df.loc[indices, modify_columns].add(vector_to_add, axis='index')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM