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]
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]
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.
You can use the pandas method nlargest
:
df['column'].nlargest(n=5)
Reference: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.nlargest.html
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).
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)