How to take 2 samples out of pd.series, using sample, so the result would be sample1+sample2=original pd.series?

Viewed 24

i need to create to random samples, in order to make cross-validation in next step. Say, we have a pd.Series object, and we are testing how it should work. But when I type this:

example=pd.Series([1,2,3,4,5,6])
example1=example.sample(n=2, replace=False, random_state=12345)
example2=example.sample(n=4, replace=False, random_state=12345)
print(example1)
print(example2)

I get:

5    6
3    4
dtype: int64
5    6
3    4
4    5
0    1
dtype: int64

but the elements shoud be different, and example 1 and example 2 shoud be equal to example. What can be done?

1 Answers

Remove values by index of example1.index, but necessary unique index values:

#if not sure if unique
#example = example.reset_index(drop=True)
example2=example.drop(example1.index).sample(n=4, replace=False, random_state=12345)

If values of Series are unique filter out by example1:

example2=example[~example.isin(example1)].sample(n=4, replace=False, random_state=12345)
Related