# Pandas: How to get N largest values of a Series

## Introduction

Pandas is a powerhouse tool for data analysis in Python, offering a wide array of functions to manipulate, analyze, and visualize data efficiently. One common task in data analysis is sorting data and finding the n largest values of a series. This tutorial will walk you through various methods to accomplish this, ranging from basic to more advanced techniques.

### Understanding Pandas Series

Before diving into the specifics, itâ€™s important to understand what a â€˜Seriesâ€™ is in Pandas. A Series is a one-dimensional labeled array capable of holding any data type. Getting the n largest values from a Series can be essential for data analysis tasks, such as identifying the top performers in a dataset, finding outliers, or simply understanding the distribution of your data.

## Basic Method: nlargest()

The most straightforward way to find the n largest values in a Series is by using the `nlargest()` method. Hereâ€™s a simple example:

``````import pandas as pd

data = {'score': [45, 82, 56, 74, 63]}
# Create a pandas Series from the data
df = pd.DataFrame(data)
score_series = df['score']
# Get the 3 largest values
largest_values = score_series.nlargest(3)
print(largest_values)
``````

Output:

``````1    82
3    74
4    63
Name: score, dtype: int64
``````

This method is efficient and straightforward, but what if we want more control or need to apply more complex conditions? Letâ€™s explore some more advanced techniques.

Another way to achieve similar results is by using the `sort_values()` method followed by `head()`. This approach gives more flexibility, as demonstrated below:

``````import pandas as pd

data = {'score': [45, 82, 56, 74, 63]}
# Create a pandas Series from the data
df = pd.DataFrame(data)
score_series = df['score']
# Sort the series in descending order and get the top 3 values
sorted_series = score_series.sort_values(ascending=False)
print(top_3)
``````

Output:

``````1    82
3    74
4    63
Name: score, dtype: int64
``````

This method not only retrieves the n largest values but also sorts the entire series, which can be helpful for further analysis.

## Using custom functions and apply()

For scenarios where predefined behaviors of `nlargest()` and `sort_values()` donâ€™t meet our needs, we can define custom functions and use the `apply()` method. Although not directly related to extracting n largest values, this technique can be powerful when combined with conditional logic to filter our Series before picking the top values.

``````import pandas as pd

def custom_filter(x):
# Custom filter logic
if x > 50:
return True
return False

data = {'score': [45, 82, 56, 74, 63]}
# Create a pandas Series from the data
df = pd.DataFrame(data)
score_series = df['score'].apply(custom_filter)
# Now use nlargest on the filtered series
filtered_largest = score_series.nlargest(3)
print(filtered_largest)
``````

Note: This code snippet will not run as expected because `apply()` returns a series of Boolean values. However, it demonstrates the idea of combining filters with extraction methods. For a working example, youâ€™d need to first filter your Series based on custom logic and then apply `nlargest()` on the filtered series.

## Using numpy for complex conditions

Another advanced technique involves using numpy alongside Pandas for more complex numerical operations. For example, if we want to find the n largest values that are also even, we could use:

``````import pandas as pd
import numpy as np

data = {'score': [45, 82, 56, 74, 63]}
# Create a pandas Series from the data
df = pd.DataFrame(data)
score_series = df['score']
# Use numpy to find even numbers
even_scores = score_series[np.mod(score_series, 2) == 0]
# Get the 3 largest even numbers
largest_even = even_scores.nlargest(3)
print(largest_even)
``````

Output:

``````1    82
3    74
2    56
Name: score, dtype: int64
``````

This method demonstrates the power of combining Pandas with numpy to apply complex numerical conditions before extracting the n largest values.

## Conclusion

Finding the n largest values in a Pandas Series is a common but crucial task in data analysis. Starting with basic methods like `nlargest()` and evolving to more complex techniques using sort_values(), custom functions, and even numpy, provides flexibility and power in your data analysis endeavors. Experiment with these methods to find the ones that best suit your needs.

Search tutorials, examples, and resources