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

Select and remove rows in pandas dataframe

I have a dataframe and want to randomly select 5 rows from it and have them in another dataframe, then remove those rows from my first dataframe.

For example in this dataframe:

enter image description here

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

Randomly select these rows:

enter image description here

And have first dataframe without them :

enter image description here

How Can I do this action?

>Solution :

You can use sample:

df1 = df.sample(n=5)
df2 = df.drop(df1.index)

Output:

>>> df1
   id color  price
9  10     b     20
6   7     b     20
8   9     g     20
7   8     g     20
1   2     r     20

>>> df2
   id color  price
0   1     r     20
2   3     y     20
3   4     y     20
4   5     g     20
5   6     r     20

If your goal is to split your dataframe in 2 equal parts, you can do:

import numpy as np

df1, df2 = np.array_split(df.sample(frac=1), 2)
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