Pandas Series.abs()


Pandas Series.abs() method

In this tutorial, we'll explore the Series.abs() method in Pandas, which is used to compute the absolute numeric value of each element in the Series.

This method is particularly useful when you want to obtain the absolute values of numerical data in a Pandas Series.

The syntax of Series.abs() method is

Series.abs()

Examples for Pandas Series.abs()

1. Finding absolute values of elements in Series

In this example, we take a Series with some numeric values. Some of them are negative. We shall use Series.abs() method to find the absolute of these values.

Python Program

import pandas as pd

# Create a Pandas Series
data = [-2, 5, -8, 10, -3]
series = pd.Series(data)

# Use abs() to get the absolute values
abs_values = series.abs()

# Display the results
print("Original Series")
print(series)
print("\nAbsolute Values")
print(abs_values)

Output

Original Series
0    -2
1     5
2    -8
3    10
4    -3
dtype: int64

Absolute Values
0     2
1     5
2     8
3    10
4     3
dtype: int64

Summary

In this tutorial, we've covered the abs() method in Pandas Series, demonstrating its use to compute the absolute values of numeric data.


Python Libraries