Explore Courses Blog Tutorials Interview Questions
0 votes
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.


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


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.


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

Browse Categories