Home > Back-end >  How to find the top any % of a dataframe?
How to find the top any % of a dataframe?

Time:10-11

I want to find the top 1% in my dataframe and append all the values in a list. Then i can check the first value inside and use it as a filter in the dataframe, any idea how to do it ? Or if you have a simplier way to do it !

You can find the dataframe i use here :

https://raw.githubusercontent.com/srptwice/forstack/main/resultat_projet.csv

What i tried is to watch my dataframe with heatmap (from Seaborn) and use a filter like that :

df4 = df2[df2 > 50700]

CodePudding user response:

You can use df.<column name>.quantile(<percentile>) to get the top % of a dataframe. For example, the code below would get you the rows for df2 where bfly column is at the top 10% (90th percentile)

import pandas as pd

df = pd.read_csv('./resultstat_projet.csv')
df.columns = df.columns.str.replace(' ', '') # remove blank spaces in columns
df2 = df[df.bfly > df.bfly.quantile(0.9)]
print(df2)
  • Related