Use pandas.DataFrame.abs().
import pandas as pd
df = pd.DataFrame(data={'count':[1, -1, 2, -2, 3, -3]})
df['count'] = df['count'].abs()
print(df)
count
#0 1
#1 1
#2 2
#3 2
#4 3
#5 3
Answer from Ffisegydd on Stack Overflow Top answer 1 of 2
106
Use pandas.DataFrame.abs().
import pandas as pd
df = pd.DataFrame(data={'count':[1, -1, 2, -2, 3, -3]})
df['count'] = df['count'].abs()
print(df)
count
#0 1
#1 1
#2 2
#3 2
#4 3
#5 3
2 of 2
1
You can call abs() on multiple columns too.
df[['col1', 'col2']] = df[['col1', 'col2']].abs()
or use numpy abs() as well, which interestingly returns a pandas object, not a numpy array:
df['col1'] = np.abs(df['col1'])
On a tangential note, if you get SettingWithCopyWarning when you convert column values into absolute values, that means your dataframe is probably created by filtering another dataframe. Turn on copy-on-write mode to turn it off. See this post for more info.
pd.options.mode.copy_on_write = True
df['count'] = df['count'].abs()
or use assign() to make a copy.
df = df.assign(count=df['count'].abs())
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.abs.html
pandas.DataFrame.abs — pandas 3.0.1 documentation
Absolute numeric values in a Series with a Timedelta element. >>> s = pd.Series([pd.Timedelta("1 days")]) >>> s.abs() 0 1 days dtype: timedelta64[us] Select rows with data closest to certain value using argsort (from StackOverflow). >>> df = pd.DataFrame( ...
Videos
04:41
Python abs() Function Example - YouTube
02:43
How To Get Absolute Values in Python - YouTube
05:31
How to get the Absolute Value in Python using abs() and Pandas ...
03:41
Write a Program to Find Absolute value of a Number in Python - YouTube
04:27
How to use Function abs() in Python - Absolute Value function in ...
Educative
educative.io › answers › how-to-obtain-the-absolute-numeric-value-of-dataframe-elements
How to obtain the absolute numeric value of DataFrame elements
In pandas, we use the abs() function to obtain the absolute numeric values of each element of a DataFrame or a Series object.
W3Schools
w3schools.com › python › pandas › ref_df_abs.asp
Pandas DataFrame abs() Method
The abs() method returns a DataFrame with the absolute value of each value.
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.DataFrame.abs.html
pandas.DataFrame.abs — pandas 2.3.3 documentation
Absolute numeric values in a Series with a Timedelta element. >>> s = pd.Series([pd.Timedelta('1 days')]) >>> s.abs() 0 1 days dtype: timedelta64[ns] Select rows with data closest to certain value using argsort (from StackOverflow). >>> df = pd.DataFrame({ ...
w3resource
w3resource.com › pandas › series › series-abs.php
Pandas Series: abs() function - w3resource
September 15, 2022 - The abs() function is used to get a Series/DataFrame with absolute numeric value of each element.
Pandas
pandas.pydata.org › pandas-docs › version › 0.23.1 › generated › pandas.DataFrame.abs.html
pandas.DataFrame.abs — pandas 0.23.1 documentation
June 20, 2022 - Absolute numeric values in a Series with a Timedelta element. >>> s = pd.Series([pd.Timedelta('1 days')]) >>> s.abs() 0 1 days dtype: timedelta64[ns] Select rows with data closest to certain value using argsort (from StackOverflow). >>> df = pd.DataFrame({ ...
DataScience Made Simple
datasciencemadesimple.com › home › get the absolute value of column in pandas python
Get the absolute value of column in pandas python - DataScience Made Simple
December 6, 2024 - 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.
Programiz
programiz.com › python-programming › pandas › methods › abs
Pandas abs()
The abs() method is used to compute the absolute value of each element in a DataFrame. The abs() method in Pandas is used to compute the absolute value of each element in a DataFrame.
Trymito
trymito.io › excel-to-python › functions › math › ABS
Excel to Python: ABS Function - A Complete Guide | Mito
To use the absolute value as part of a more complex operation, you can use the `apply()` function to apply the operation to every element in an pandas dataframe column. # Define a function to calculate the absolute sum of a row def abs_sum(row): return row.abs().sum() # Create a new column 'ABS_SUM' by applying the custom function df['ABS_SUM'] = df.apply(abs_sum, axis=1) ... When implementing the ABS function in Python, there are a few common challenges that you might run into.
Pythontic
pythontic.com › pandas › dataframe-computations › abs
Finding absolute values for a pandas dataframe object | Pythontic.com
May 11, 2025 - The absolute value of a complex number is given by the square root of the sum of squares of the real and the complex parts. DataFrame.abs() method finds the absolute value for each of the numeric element present in a DataFrame and returns them as another DataFrame.
Top answer 1 of 3
1
You can call .abs() afterwards:
df["Discrepancy_Num"] = (
(df["Impressions_Source"] - df["Impressions_Source2"])
/ (df["Impressions_Source"] * 100)
).abs()
print(df)
Prints:
id Impressions_Source Impressions_Source2 Discrepancy_Num
0 15020 150201 151920 0.000114
2 of 3
1
I would go for
df["Discrepancy_Num"] = (
-1 + df.Impressions_Source2/df.Impressions_Source
).abs()/100
since you can avoid accessing df.Impressions_Source twice. Indeed, the result is the same as yours, stemming from a rearranged formulation.
Pandas
pandas.pydata.org › docs › dev › reference › api › pandas.DataFrame.abs.html
pandas.DataFrame.abs — pandas 3.0.0.dev0+2406.gbc500f7b44 documentation
Absolute numeric values in a Series with a Timedelta element. >>> s = pd.Series([pd.Timedelta("1 days")]) >>> s.abs() 0 1 days dtype: timedelta64[ns] Select rows with data closest to certain value using argsort (from StackOverflow). >>> df = pd.DataFrame( ...
TutorialKart
tutorialkart.com › python › pandas › pandas-dataframe-abs
Absolute Function to each Element of DataFrame in Pandas
November 6, 2024 - To compute the absolute value of each element of DataFrame in Pandas, call abs() method on this DataFrame.
Moonbooks
moonbooks.org › Articles › How-to-get-the-absolute-value-of-a-dataframe-column-in-pandas-
How to get the absolute value of a dataframe column in pandas ?
November 29, 2021 - Tags: Python; Pandas; DataFrame; Examples of how to get the absolute value of a dataframe column in pandas: Table of contents · Create a dataframe with pandas · Apply abs() to one dataframe column · Apply abs() to multiple dataframe columns · Use abs() to filter the data ·