Youkesen
Youkesen

Reputation: 167

How to get top 5 values from pandas dataframe?

I am new to pandas and I would like to filter a dataframe in pandas that includes the top 5 values in the list. What is the best way to get the 5 values from the list with that code?

My Code:

cheese_top5 = cheese[cheese.year >= 2016]

Upvotes: 7

Views: 91345

Answers (6)

will roberts
will roberts

Reputation: 1

Top 5 values in a column called 'Column_name' in a dataframe called 'df'.

method 1:

df.sort_values('Column_name', ascending=False).head(5)

method 2:

df['Column_name'].nlargest(n=5)

Upvotes: 0

Sumit Anand
Sumit Anand

Reputation: 21

To get the top 5 most occuring values use

df['column'].value_counts().head(n)

The solution provided by @lux7

df['column'].nlargest(n=5)

would result in the top 5 values from a column(their values not how many times they have appeared).

Upvotes: 0

lux7
lux7

Reputation: 2150

You can use the pandas method nlargest:

df['column'].nlargest(n=5)

Reference: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.nlargest.html

Upvotes: 14

user12578371
user12578371

Reputation:

import pandas as pd
df = pd.read_csv('911.csv')
df['zip'].value_counts().head(5)

Upvotes: 1

ashish mishra
ashish mishra

Reputation: 11

dataframe_name['field_name'].value_counts.head(5)

Upvotes: 1

Mark
Mark

Reputation: 984

I think what you are looking for is:

cheese.sort_values(by=['Name of column']).head(5)

to say anything more we need to see a sample of your data.

Upvotes: 15

Related Questions