Get the absolute value of column in pandas python

abs() is the function used to get the absolute value of column in pandas python.  With an Example we will see on how to get absolute value of column in pandas dataframe. and absolute value of the series in pandas.

Get the absolute value of column in pandas python 1

Let’s see how to

    • Get the absolute value of column in pandas python
    • Get absolute value of the series in pandas

Let’s first create the dataframe.

import pandas as pd
import numpy as np

#Create a DataFrame
df1 = {
    'Subject':['semester1','semester2','semester3','semester4','semester1',
               'semester2','semester3'],
   'Score':[-62,47,-55,-74,31,77,-85]}
df1 = pd.DataFrame(df1,columns=['Subject','Score'])
print(df1)

So the resultant dataframe will be

Get the absolute value of column in pandas python 1

Let’s get the absolute value of a column in pandas dataframe with abs function as shown below


df1['Absolute_Score']= abs(df1['Score'])
print(df1)

So the result will be

Get the absolute value of column in pandas python 2

 

 

Absolute Value of the Series in Pandas:

import pandas as pd
import numpy as np
 
## Create Series in pandas
s = pd.Series([-4.8, 7, -5.2, -2,6])

## Absolute value of series in pandas
s.abs()

So the absolute value of the series in pandas will be,

Get the absolute value of column in pandas python 2

Get the absolute value of column in pandas python                                                                                                           Get the absolute value of column in pandas python n

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts