Open In App

Get the absolute values in Pandas

Improve
Improve
Like Article
Like
Save
Share
Report

Let us see how to get the absolute value of an element in Python Pandas. We can perform this task by using the abs() function. The abs() function is used to get a Series/DataFrame with absolute numeric value of each element.

Syntax : Series.abs() or DataFrame.abs()
Parameters : None
Returns : Series/DataFrame containing the absolute value of each element.

Example 1 : Absolute numeric values in a Series.




# import the library
import pandas as pd
  
# create the Series
s = pd.Series([-2.8, 3, -4.44, 5])
print(s)
  
# fetching the absolute values
print("\nThe absolute values are :")
print(s.abs())


Output :

Example 2 : Absolute numeric values in a Series with complex numbers.




# import the library
import pandas as pd
  
# create the Series
s = pd.Series([2.2 + 1j])
print(s)
  
# fetching the absolute values
print("\nThe absolute values are :")
print(s.abs())


Output :

Example 3 : Absolute numeric values in a Series with a Timedelta element.




# import the library
import pandas as pd
  
# create the Series
s = pd.Series([pd.Timedelta('2 days')])
print(s)
  
# fetching the absolute values
print("\nThe absolute values are :")
print(s.abs())


Output :

Example 4 : Fetching the absolute values from a DataFrame column.




# import the library
import pandas as pd
  
# create the DataFrame
df = pd.DataFrame({'p' : [2, 3, 4, 5],
                   'q' : [10, 20, 30, 40],
                   'r' : [200, 60, -40, -60]})
display(df)
  
# fetching the absolute values
print("\nThe absolute values are :")
display(df.r.abs())


Output :



Last Updated : 18 Aug, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads