Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
nlargest() method is used to get n largest values from a data frame or a series.
DataFrame.nlargest(n, columns, keep='first')
n: int, Number of values to select
columns: Column to check for values or user can select column while calling too. [For example: data[“age”].nsmallest(3) OR data.nsmallest(3, “age”)]
keep: object to set which value to select if duplicates exit. Options are ‘first’ or ‘last’
To download the CSV file used, Click Here.
Code #1: Extracting Largest 5 values
In this example, Largest 5 values are extracted and then compared to the other sorted by the sort_values() function. NaN values are removed before trying this method.
Code #2: Sorting by sort_values()
As shown in the output image, the values returned by both functions is similar.
- Python | pandas.map()
- Python | Pandas dataframe.mad()
- Python | Pandas.CategoricalDtype()
- Python | Pandas dataframe.get()
- Python | Pandas Series.sem()
- Python | Pandas PeriodIndex.day
- Python | Pandas dataframe.eq()
- Python | Pandas Panel.sum()
- Python | Pandas Series.mean()
- Python | Pandas Panel.abs()
- Python | Pandas.Categorical()
- Python | Pandas Series.pop()
- Python | Pandas Timestamp.dst
- Python | Pandas Series.ptp()
- Python | Pandas Timestamp.tz
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.