let's say I have a pandas dataframe, I want to select a column of the dataframe and add values to its existing values randomly, in other words, I want to select random values from that column and add some constant to them. What I did is I selected a sample with
df['column_in_question'].sample(frac=0.2, random_state=1).values 1000
but this command only generates a list of the values and add 1000 to them, that's not the behavior that I want.
CodePudding user response:
You can get the sampled indexes and adding value by selecting with these indexes
indexes = df['column_in_question'].sample(frac=0.2, random_state=1).index
df.loc[indexes, 'column_in_question'] = 1000
# or
df['Number'] = df['Number'].mask(df.index.isin(indexes), df['Number'].add(1000))
# or
import numpy as np
df['Number'] = np.where(df.index.isin(indexes), df['Number'].add(1000), df['Number'])