Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I have a data set with 36k rows. I want to randomly select 9k rows from it using pandas. How do I accomplish this task?

1 Answer

0 votes
by (41.4k points)
edited by

Let’s understand random selection of rows from a data set using pandas.

we can use sample method that returns a random sample of items from the dataframe.

df.sample()

You have to use parameter k which selects n number of rows randomly.

df.sample(k=5)

After that, using frac parameter, you can select a fraction of items and select a row. If frac=0.5 then the method returns 50% of rows.

df.sample(frac=0.5)  

So, according to you question

You can use df.sample(n=9000)or df.sample(frac=0.25) for to select 9k rows randomly.

If you want to be build successful data science career then enroll for data science certification.

Related questions

0 votes
1 answer
asked Jul 10, 2019 in SQL by Tech4ever (20.3k points)
+3 votes
2 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...