BeeGee
BeeGee

Reputation: 875

Randomly assign values to subset of rows in pandas dataframe

I am using Python 2.7.11 with Anaconda.

I understand how to set the value of a subset of rows of a Pandas DataFrame like Modifying a subset of rows in a pandas dataframe, but I need to randomly set these values.

Say I have the dataframe df below. How can I randomly set the values of group == 2 so they are not all equal to 1.0?

import pandas as pd
import numpy as np

df = pd.DataFrame([1,1,1,2,2,2], columns = ['group'])
df['value'] = np.nan
df.loc[df['group'] == 2, 'value'] = np.random.randint(0,5)

print df
   group  value
0      1    NaN
1      1    NaN
2      1    NaN
3      2    1.0
4      2    1.0
5      2    1.0

df should look something like the below:

print df
   group  value
0      1    NaN
1      1    NaN
2      1    NaN
3      2    1.0
4      2    4.0
5      2    2.0

Upvotes: 4

Views: 3026

Answers (1)

piRSquared
piRSquared

Reputation: 294506

You must determine the size of group 2

g2 = df['group'] == 2
df.loc[g2, 'value'] = np.random.randint(5, size=g2.sum())
print(df)

   group  value
0      1    NaN
1      1    NaN
2      1    NaN
3      2    3.0
4      2    4.0
5      2    2.0

Upvotes: 5

Related Questions