Python | Pandas DataFrame.abs ()

Python Methods and Functions

dataframe.abs() — one of the simplest pandas data functions. It returns an object with the resulting absolute value and only applies to objects that are numeric. It also doesn`t work with any Nan values. The abs () function can also be used with complex numbers to find their absolute value.

For complex numbers, the absolute value is defined as:

  Syntax:  DataFrame.abs ()  Returns:  type of caller  

To link to the CSV file used in the code, click here

Example # 1: replace Boston Celtics with Omega Warrior in nba.csv

# import pandas as pd

import pandas as pd

 
# Create data frame from CSV file

df = pd.read_csv ( "nba .csv " )

  
# Print the first 10 lines
# data frame to render

df [: 10 ]

To find the absolute value, we also need have negative values ​​in the data frame. So let`s change some of the values ​​to negative for demonstration purposes.

# This will set the column number
# be all negative.

df.Number = df.Number * ( - 1 )

Output:

Now let`s use the abs () function to find only the absolute value of the Number column.

# Applying the abs () value t Only to one column

df.Number. abs ()

Output:

Example # 2: Using abs ( ) to a series with complex numbers.

# Import pandas as pd

import pandas as pd

  
# Create series

ser = pd.Series ([ 1.2 + 1j , 2 + 5j , 1 + 8j , 3.2 + 2j ])

 
# print the values ​​in ser
ser

# Using the abs () function to find
# absolute value of complex numbers

absolute_values ​​ = s. abs () < / code>

 
# Print the absolute values ​​of all complex numbers
absolute_values ​​





Get Solution for free from DataCamp guru