I have a pandas dataframe df
which appears as following:
Month Day mnthShape
1 1 1.016754224
1 1 1.099451003
1 1 0.963911929
1 2 1.016754224
1 1 1.099451003
1 2 0.963911929
1 3 1.016754224
1 3 1.099451003
1 3 1.783775568
I want to get the following from df
:
Month Day mnthShape
1 1 1.016754224
1 2 1.016754224
1 3 1.099451003
where the mnthShape
values are selected at random from the index. i.e. if the query is df.loc[(1, 1)] it should look for all values for (1, 1) and select randomly from it a value to be displayed above.
One way is to
Series.sample()
a random row from each group:Use
groupby
withapply
to select a row at random per group.If you want to know what index the sampled rows come from, use
pd.Series.sample
withn=1
: