Python | Pandas DataFrame.abs()
Last Updated :
09 Mar, 2022
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
dataframe.abs() is one of the simplest pandas dataframe function. It returns an object with absolute value taken and it is only applicable to objects that are all numeric. It does not work with any Nan value either. 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
For link to CSV file Used in Code, click here
Example #1: Replace team “Boston Celtics” with “Omega Warrior” in the nba.csv file
Python3
import pandas as pd
df = pd.read_csv("nba.csv")
df[: 10 ]
|
In order to find the absolute value, we also need to have negative values in the dataframe. So, let’s change some values to be negative for the demonstration purpose.
Python3
df.Number = df.Number * ( - 1 )
|
Output:
Now let’s use abs() function to find only the absolute value of the Number column.
Output:
Example #2: Applying abs() on a series with complex numbers.
Python3
import pandas as pd
ser = pd.Series([ 1.2 + 1j , 2 + 5j , 1 + 8j , 3.2 + 2j ])
ser
|
Python3
absolute_values = s. abs ()
absolute_values
|
Share your thoughts in the comments
Please Login to comment...