Reputation: 1470
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
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
Reputation: 12417
This should work:
your_percenteage = 5 #or 20, 30 etc
df = df.iloc[:round(len(df)/100*your_percentage)]
Upvotes: 2