I want to partition a pandas DataFrame into ten disjoint, equally-sized, randomly composed subsets.
I know I can randomly sample one tenth of the original pandas DataFrame using:
partition_1 = pandas.DataFrame.sample(frac=(1/10))
However, how can I obtain the other nine partitions? If I'd do pandas.DataFrame.sample(frac=(1/10))
again, there exists the possibility that my subsets are not disjoint.
Thanks for the help!
Starting with this.
dfm = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar', 'foo', 'bar', 'foo', 'foo']*2,
'B' : ['one', 'one', 'two', 'three', 'two', 'two', 'one', 'three']*2})
A B
0 foo one
1 bar one
2 foo two
3 bar three
4 foo two
5 bar two
6 foo one
7 foo three
8 foo one
9 bar one
10 foo two
11 bar three
12 foo two
13 bar two
14 foo one
15 foo three
Usage:
Change "4" to "10", use [i] to get the slices.
np.random.seed(32) # for reproducible results.
np.array_split(dfm.reindex(np.random.permutation(dfm.index)),4)[1]
A B
2 foo two
5 bar two
10 foo two
12 foo two
np.array_split(dfm.reindex(np.random.permutation(dfm.index)),4)[3]
A B
13 foo two
11 bar three
0 foo one
7 foo three
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With