Hana
Hana

Reputation: 1470

How can I efficiently get the first x% of a DataFrame?

Say I have a DataFrame with 1000 rows. If I wish to create a series of only the first 5% (or the first 50 rows) what is the best way to do this in terms of percentages? (I don't want to simply do df.head(50)) I would like the code to able to adapt I wanted to change x to say 20% or 30%.

Upvotes: 2

Views: 87

Answers (2)

Aaron Brock
Aaron Brock

Reputation: 4536

All you need to do is calculate the percenteage before you you call .head()

Example:

percenteage = 20
rows_to_keep = round(percenteage / 100 * len(df))
df = df.head(rows_to_keep)

Upvotes: 2

Joe
Joe

Reputation: 12417

This should work:

your_percenteage = 5 #or 20, 30 etc
df = df.iloc[:round(len(df)/100*your_percentage)]

Upvotes: 2

Related Questions