Select and remove rows in pandas dataframe

Advertisements

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:

Randomly select these rows:

And have first dataframe without them :

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)

Leave a ReplyCancel reply