I have a simple df forming pivot_table:
d = {'one' : ['A', 'B', 'B', 'C', 'C', 'C'], 'two' : [6., 5., 4., 3., 2., 1.], 'three' : [6., 5., 4., 3., 2., 1.], 'four' : [6., 5., 4., 3., 2., 1.]}
df = pd.DataFrame(d)
pivot = pd.pivot_table(df,index=['one','two'])
I would like to randomly sample 1 row from each other element from the column "one" of the resulting rotation object. (In this example, “A” will always be selected, while there are more options for “B” and “C”.) I just started using pandas version 0.18.0 and I know about .sample . I messed up the .groupby method using the fetch function like this:
grouped = pivot.groupby('one').apply(lambda x: x.sample(n=1, replace=False))
I raise KeyError when I tried variations of this theme, so I thought it was time for a new perspective on this seemingly simple question ...
Thanks for any help!