Skip to content Skip to sidebar Skip to footer

Random Selection In Pandas Dataframe

I'm trying to solve this more complicated question. Here's a smaller problem: Given df a b 1 2 5 0 5 9 3 6 1 8 How can I create a column C that is a random selec

Solution 1:

import random

n = 2  # target row number
random.sample(df.iloc[n, :2], 1)  # Pick one number from this row.

For the whole dataframe:

>>> df.loc[:, ['a', 'b']].apply(random.sample, args=(1,), axis=1)
0[1]1[5]2[9]3[3]4[8]dtype: object

Cleaning it up to extract the single values:

>>>pd.Series([i[0] for i in df.loc[:, ['a', 'b']].apply(random.sample, args=(1,), axis=1)], index=df.index)
0    1
1    5
2    9
3    3
4    8
dtype: int64

Or taking advantage that column 'a' is indexed at zero (False) and column 'b' is indexed at 1 (True):

>>>[df.iat[i, j] for i, j inenumerate(1 * (np.random.rand(len(df)) < .5))]
[1, 5, 5, 6, 8]

Solution 2:

No need to use a separate random module:

s = """a    b
1    2
5    0
5    9
3    6
1    8
"""

df = pd.read_table(StringIO(s),sep='\s+',engine='python')
df.apply(lambda x: x.sample(n=1).iloc[0],axis=1)
#output:0115293641
dtype: int64

Post a Comment for "Random Selection In Pandas Dataframe"