Navneet kashyap
Navneet kashyap

Reputation: 23

Random Sample From Data frame and remains

How to select remains of data frame after random selection of data?

This will give 80% data. but I want remaining 20% also.

df.sample(frac=0.8)

Upvotes: 2

Views: 1783

Answers (2)

acrobat
acrobat

Reputation: 917

You can use:

df_sample = df.sample(frac=0.8)

and then:

df_remains = df[~df.index.isin(df_sample.index)]

Upvotes: 3

user3659451
user3659451

Reputation: 2073

Since you also have numpy installed, a Pandas dependency, you can do something like this:

import numpy as np

p = .8
msk = np.random.rand(len(df)) < p
sample = df[msk]
remains = df[~msk]

Upvotes: 1

Related Questions