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.
Answer from Mark on Stack Overflow Top answer 1 of 6
15
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.
2 of 6
15
You can use the pandas method nlargest:
df['column'].nlargest(n=5)
Reference: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.nlargest.html
Pandas
pandas.pydata.org › pandas-docs › version › 0.17.0 › generated › pandas.DataFrame.nlargest.html
pandas.DataFrame.nlargest — pandas 0.17.0 documentation
Get the rows of a DataFrame sorted by the n largest values of columns.
Learn EASY STEPS
learneasysteps.com › home › python › how to calculate top 5 max values in pandas
How to calculate top 5 max values in Pandas - Learn EASY STEPS
August 15, 2021 - The function that is helpful for finding the Top 5 maximum value is nlargest(). The below article explains with the help of an example How to calculate Top 5 max values by Group in Pandas Python. John has store sales data available for analysis. There are five columns present in the data, Geography (country of store), Department (Industry category of the store), StoreID (Unique ID of each store), Time Period (Month of sales), Revenue (Total Sales for the month).
Top answer 1 of 8
89
To get the highest values of a column you can try nlargest() :
df['High'].nlargest(2)
The above will give you the 2 highest values of column High.
You can also use nsmallest() to get the lowest values.
2 of 8
22
Here is a NumPy solution:
In [120]: df
Out[120]:
a b c d e f g h
0 1.334444 0.322029 0.302296 -0.841236 -0.360488 -0.860188 -0.157942 1.522082
1 2.056572 0.991643 0.160067 -0.066473 0.235132 0.533202 1.282371 -2.050731
2 0.955586 -0.966734 0.055210 -0.993924 -0.553841 0.173793 -0.534548 -1.796006
3 1.201001 1.067291 -0.562357 -0.794284 -0.554820 -0.011836 0.519928 0.514669
4 -0.243972 -0.048144 0.498007 0.862016 1.284717 -0.886455 -0.757603 0.541992
5 0.739435 -0.767399 1.574173 1.197063 -1.147961 -0.903858 0.011073 -1.404868
6 -1.258282 -0.049719 0.400063 0.611456 0.443289 -1.110945 1.352029 0.215460
7 0.029121 -0.771431 -0.285119 -0.018216 0.408425 -1.458476 -1.363583 0.155134
8 1.427226 -1.005345 0.208665 -0.674917 0.287929 -1.259707 0.220420 -1.087245
9 0.452589 0.214592 -1.875423 0.487496 2.411265 0.062324 -0.327891 0.256577
In [121]: np.sort(df.values)[:,-2:]
Out[121]:
array([[ 1.33444404, 1.52208164],
[ 1.28237078, 2.05657214],
[ 0.17379254, 0.95558613],
[ 1.06729107, 1.20100071],
[ 0.86201603, 1.28471676],
[ 1.19706331, 1.57417327],
[ 0.61145573, 1.35202868],
[ 0.15513379, 0.40842477],
[ 0.28792928, 1.42722604],
[ 0.48749578, 2.41126532]])
or as a pandas Data Frame:
In [122]: pd.DataFrame(np.sort(df.values)[:,-2:], columns=['2nd-largest','largest'])
Out[122]:
2nd-largest largest
0 1.334444 1.522082
1 1.282371 2.056572
2 0.173793 0.955586
3 1.067291 1.201001
4 0.862016 1.284717
5 1.197063 1.574173
6 0.611456 1.352029
7 0.155134 0.408425
8 0.287929 1.427226
9 0.487496 2.411265
or a faster solution from @Divakar:
In [6]: df
Out[6]:
a b c d e f g h
0 0.649517 -0.223116 0.264734 -1.121666 0.151591 -1.335756 -0.155459 -2.500680
1 0.172981 1.233523 0.220378 1.188080 -0.289469 -0.039150 1.476852 0.736908
2 -1.904024 0.109314 0.045741 -0.341214 -0.332267 -1.363889 0.177705 -0.892018
3 -2.606532 -0.483314 0.054624 0.979734 0.205173 0.350247 -1.088776 1.501327
4 1.627655 -1.261631 0.589899 -0.660119 0.742390 -1.088103 0.228557 0.714746
5 0.423972 -0.506975 -0.783718 -2.044002 -0.692734 0.980399 1.007460 0.161516
6 -0.777123 -0.838311 -1.116104 -0.433797 0.599724 -0.884832 -0.086431 -0.738298
7 1.131621 1.218199 0.645709 0.066216 -0.265023 0.606963 -0.194694 0.463576
8 0.421164 0.626731 -0.547738 0.989820 -1.383061 -0.060413 -1.342769 -0.777907
9 -1.152690 0.696714 -0.155727 -0.991975 -0.806530 1.454522 0.788688 0.409516
In [7]: a = df.values
In [8]: a[np.arange(len(df))[:,None],np.argpartition(-a,np.arange(2),axis=1)[:,:2]]
Out[8]:
array([[ 0.64951665, 0.26473378],
[ 1.47685226, 1.23352348],
[ 0.17770473, 0.10931398],
[ 1.50132666, 0.97973383],
[ 1.62765464, 0.74238959],
[ 1.00745981, 0.98039898],
[ 0.5997243 , -0.0864306 ],
[ 1.21819904, 1.13162068],
[ 0.98982033, 0.62673128],
[ 1.45452173, 0.78868785]])
ProjectPro
projectpro.io › recipes › find-largest-value-in-pandas-dataframe
How to find the largest value in a Pandas DataFrame? -
May 11, 2022 - We have created a dictionary of data and passed it in pd.DataFrame to make a dataframe with columns 'first_name', 'last_name', 'age', 'Comedy_Score' and 'Rating_Score'. raw_data = {'first_name': ['Sheldon', 'Raj', 'Leonard', 'Howard', 'Amy'], 'last_name': ['Copper', 'Koothrappali', 'Hofstadter', 'Wolowitz', 'Fowler'], 'age': [42, 38, 36, 41, 35], 'Comedy_Score': [9, 7, 8, 8, 5], 'Rating_Score': [25, 25, 49, 62, 70]} df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age', 'Comedy_Score', 'Rating_Score']) print(df) Explore More Data Science and Machine Learning Projects for Practice. Fast-Track Your Career Transition with ProjectPro · We can get the index of largest value by using the function idxmax and lowest value by idxmin.
Data Science Discovery
discovery.cs.illinois.edu › guides › DataFrame-Row-Selection › finding-min-and-max
Finding Minimum and Maximum Values in a DataFrame Column - Data Science Discovery
August 10, 2022 - The nlargest and nsmallest functions allow us to find the rows that have the largest or smallest values for a specific columns. For example, we may want the three movies with the largest domestic box office sales: import pandas as pd\n \n#Creates a DataFrame of "movie", "release date", "domestic gross", "worldwide gross", "personal rating", and "international box office" columns\ndf = pd.DataFrame([\n {"movie": "The Truman Show", "release date": "1996-06-05", "domestic box office": 125618201, "worldwide box office": 264118201, "personal rating": 10, "international box office": 138500000},
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.nlargest.html
pandas.DataFrame.nlargest — pandas 3.0.2 documentation
>>> df.nlargest(5, "population", keep="all") population GDP alpha-2 France 65000000 2583560 FR Italy 59000000 1937894 IT Malta 434000 12011 MT Maldives 434000 4520 MV Brunei 434000 12128 BN · To order by the largest values in column “population” and then “GDP”, we can specify multiple ...
GeeksforGeeks
geeksforgeeks.org › get-n-largest-values-from-a-particular-column-in-pandas-dataframe
Get n-largest values from a particular column in Pandas DataFrame - GeeksforGeeks
December 18, 2018 - Letâs see how to Select rows based on some conditions in Pandas DataFrame. Selecting rows based on particular column value using '>', '=', '=', '<=', '!=' operator. Code #1 : Selecting all the rows from the given dataframe in which 'Percentage' is greater than 80 using basic method.
W3Schools
w3schools.com › python › pandas › ref_df_max.asp
Pandas DataFrame max() Method
Pandas HOME Pandas Intro Pandas Getting Started Pandas Series Pandas DataFrames Pandas Read CSV Pandas Read JSON Pandas Analyzing Data · Cleaning Data Cleaning Empty Cells Cleaning Wrong Format Cleaning Wrong Data Removing Duplicates · Pandas Correlations · Pandas Plotting · Pandas Certificate · Pandas Editor Pandas Quiz Pandas Exercises Pandas Syllabus Pandas Study Plan · DataFrames Reference · ❮ DataFrame Reference · Return the highest value for each column: import pandas as pd data = [[10, 18, 11], [13, 15, 8], [9, 20, 3]] df = pd.DataFrame(data) print(df.max()) Try it Yourself » ·
Data Science Dojo
discuss.datasciencedojo.com › python
How to get the n’th largest value of a column? - Python - Data Science Dojo Discussions
April 19, 2023 - I am seeking the expertise of the programming community for ways to obtain the nth largest value of a column in a Pandas dataframe using Python. While it is possible to extract the nth largest value by sorting the column in descending order and indexing it with the nth position, this can be ...
Educative
educative.io › answers › how-to-get-row-number-of-nth-largest-value-in-column-in-pandas
How to get row number of nth largest value in column in pandas
In this method, we first initialize the pandas dataframe. Then we use argsort(), which gives the indices of sorted column of dataframe. Then, we get the row number of the nth largest value from the indices list.
Top answer 1 of 4
14
you want to use columns parameter:
In [53]: df.nlargest(5, columns=['C'])
Out[53]:
A B C D
17 43 91 95 32
18 13 36 81 56
7 61 90 76 85
16 68 21 73 68
14 3 64 71 59
2 of 4
4
without using nlargest, by using sort_values
df.sort_values('C',ascending=False).iloc[:5,]
or using head
df.sort_values('C',ascending=False).head(5)
or using quantile
df[df.C>df.C.quantile(1-(5/len(df)))]
TutorialsPoint
tutorialspoint.com › article › python-pandas-find-the-maximum-value-of-a-column-and-return-its-corresponding-row-values
Python Pandas – Find the maximum value of a column and return its corresponding row values
August 27, 2023 - import pandas as pd df = pd.DataFrame( { "x": [5, 2, 7, 0], "y": [4, 7, 5, 1], "z": [9, 3, 5, 1] } ) col = "x" max_row = df.loc[df[col].idxmax()] print("Maximum value of column", col, "and its corresponding row values:") print(max_row)
Skytowner
skytowner.com › explore › selecting_top_n_rows_with_the_largest_values_for_a_column_in_pandas
Selecting top n rows with the largest values for a column in Pandas
Python●Pandas Tags · tocTable of Contents expand_more · Example mode_heat · Master the mathematics behind data science with 100+ top-tier guides Start your free 7-days trial now! To select the top n rows with the largest values in a column, use the DataFrame's nlargest(~) method.