Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dataset splitting with pandas sample and drop does not work as expected

I have a train dataset with 4,000 examples, I want to split it randomly into 2 equal sub-datasets with 2,000 in each of them. As suggested here i used the split and drop methods like so:

I1 = train_df.sample(frac=0.5, random_state=opts.seed)
I2 = train_df.drop(index=I1.index)

However it seems like it drops more indices for no apparent reason:

print(len(train_df))
print(len(I1))
print(len(I2))

4000
2000
1010

I would appreciate any insight as to why it happens.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

For me, it’s just that train_df has multiple same indexes. This should work as expected:

train_df = train_df.reset_index(drop=True)
I1 = train_df.sample(frac=0.5, random_state=opts.seed)
I2 = train_df.drop(index=I1.index)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading