How to get top 5 values from pandas dataframe?

Viewed 42430

I am new to pandas and I would like to filter a dataframe in pandas which includes top5 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]
5 Answers

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.

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

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).

Related