Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas - get first n-rows based on percentage

I have a dataframe i want to pop certain number of records, instead on number I want to pass as a percentage value.

for example,

df.head(n=10)

Pops out first 10 records from data set. I want a small change instead of 10 records i want to pop first 5% of record from my data set. How to do this in pandas.

I'm looking for a code like this,

df.head(frac=0.05)

Is there any simple way to get this?

like image 353
Mohamed Thasin ah Avatar asked May 04 '18 10:05

Mohamed Thasin ah


People also ask

How do you select the first n rows in a data frame?

You can use df. head() to get the first N rows in Pandas DataFrame. Alternatively, you can specify a negative number within the brackets to get all the rows, excluding the last N rows.

How do you extract the first 10 rows in pandas?

Use pandas. DataFrame. head(n) to get the first n rows of the DataFrame. It takes one optional argument n (number of rows you want to get from the start).

How do you do percentages in pandas?

You can caluclate pandas percentage with total by groupby() and DataFrame. transform() method. The transform() method allows you to execute a function for each value of the DataFrame. Here, the percentage directly summarized DataFrame, then the results will be calculated using all the data.

What is Nlargest in pandas?

Pandas DataFrame nlargest() Method The nlargest() method returns a specified number of rows, starting at the top after sorting the DataFrame by the highest value for a specified column.


1 Answers

I want to pop first 5% of record

There is no built-in method but you can do this:

You can multiply the total number of rows to your percent and use the result as parameter for head method.

n = 5
df.head(int(len(df)*(n/100)))

So if your dataframe contains 1000 rows and n = 5% you will get the first 50 rows.

like image 120
Mihai Alexandru-Ionut Avatar answered Sep 24 '22 08:09

Mihai Alexandru-Ionut