Pandas Series abs() Function:
Pandas abs: The abs() function of the Pandas Series returns a Series with each element’s absolute numeric value. This function only works with all-numerical elements.
Syntax:
Series.abs()
Parameters: This function doesn’t accept any parameters
Return Value:
A Series or a DataFrame that contains the absolute value of each element is returned.
- Python Pandas Series std() Function
- Python Pandas Series var() Function
- Python Pandas Series skew() Function
Pandas Series abs() Function in Python
Example1
Pandas absolute value: Here, the abs() function returns a Series with absolute numeric values for each element.
Approach:
- Import pandas module using the import keyword.
- Pass some random list as an argument to the Series() function of the pandas module to create a series.
- Store it in a variable.
- Print the above-given series.
- Apply abs() function on the given series to get the absolute values of all the elements of the given series and print the result.
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword. import pandas as pd # Pass some random list as an argument to the Series() function # of the pandas module to create a series. # Store it in a variable. gvn_series = pd.Series([-12, 40, -65, 0, -3]) # Print the above given series print("The given series is:") print(gvn_series) print() # Apply abs() function on the given series to get the # absolute values of all the elements of the given series # and print the result. print("The absolute values of all the elements of the given series:") print(gvn_series.abs())
Output:
The given series is: 0 -12 1 40 2 -65 3 0 4 -3 dtype: int64 The absolute values of all the elements of the given series: 0 12 1 40 2 65 3 0 4 3 dtype: int64
Example2: On a Specified Series in a Dataframe
Here, the abs() function is applied to a particular series or column of a dataframe given.
Approach:
- Import pandas module using the import keyword.
- Pass some random key-value pair(dictionary) as arguments to the DataFrame() function of the pandas module to create a dataframe.
- Store it in a variable.
- Print the given dataframe.
- Apply abs() function on the gvn_list1 series/column of the dataframe to get the absolute values of all the elements of the gvn_list1 series and print the result.
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword. import pandas as pd # Pass some random key-value pair(dictionary) as arguments to the # DataFrame() function of the pandas module to create a dataframe # Store it in a variable. data_frme = pd.DataFrame({ "gvn_list1": [-4, 20, 5, -2], "gvn_list2": [5, -7, -15, -13] }) # Print the given dataframe print("The given Dataframe:") print(data_frme) print() # Apply abs() function on the gvn_list1 series/column of the dataframe # to get the absolute values of all the elements of the gvn_list1 series # and print the result. print("The absolute values of gvn_list1 series of the dataframe:") print(data_frme["gvn_list1"].abs())
Output:
The given Dataframe: gvn_list1 gvn_list2 0 -4 5 1 20 -7 2 5 -15 3 -2 -13 The absolute values of gvn_list1 series of the dataframe: 0 4 1 20 2 5 3 2 Name: gvn_list1, dtype: int64