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 OverflowPandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.abs.html
pandas.DataFrame.abs — pandas 3.0.1 documentation
For complex inputs, 1.2 + 1j, the absolute value is \(\sqrt{ a^2 + b^2 }\).
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())
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 ...
W3Schools
w3schools.com › python › pandas › ref_df_abs.asp
Pandas DataFrame abs() Method
A DataFrame object with absolute values. This function does NOT make changes to the original DataFrame object. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
w3resource
w3resource.com › pandas › series › series-abs.php
Pandas Series: abs() function - w3resource
Example - Absolute numeric values in a Series: Python-Pandas Code: import numpy as np import pandas as pd s = pd.Series([-2.8, 3, -4.44, 5]) s.abs() Output: 0 2.80 1 3.00 2 4.44 3 5.00 dtype: float64 · Example - Absolute numeric values in a Series with complex numbers: Python-Pandas Code: import numpy as np import pandas as pd s = pd.Series([2.2 + 1j]) s.abs() Output: 0 2.416609 dtype: float64 ·
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.abs.html
pandas.Series.abs — pandas 3.0.1 documentation
For complex inputs, 1.2 + 1j, the absolute value is \(\sqrt{ a^2 + b^2 }\).
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.DataFrame.abs.html
pandas.DataFrame.abs — pandas 2.3.3 documentation
For complex inputs, 1.2 + 1j, the absolute value is \(\sqrt{ a^2 + b^2 }\).
Pythontic
pythontic.com › pandas › dataframe-computations › abs
Finding absolute values for a pandas dataframe object | Pythontic.com
abs() function of DataFrame class computes modulus or absolute value for the values present in a DataFrame instance. The pandas example has a dataframe with integers, rational numbers and complex numbers for which the absolute values are computed..
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.
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
February 5, 2023 - 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()
Programiz
programiz.com › python-programming › pandas › methods › abs
Pandas abs()
Become a certified Python programmer. Try Programiz PRO! ... The abs() method in Pandas is used to compute the absolute value of each element in a DataFrame.
IncludeHelp
includehelp.com › python › absolute-value-for-a-column.aspx
Python - Absolute value for a column
We will use the .abs() method to convert the actual value into absolute value. ... # Importing pandas package import pandas as pd # Creating a dictionary d = { 'a':[2,4,6,8,10,2], 'b':[1,2,3,4,5,6], 'c':[1, -1, 2, -2, 3, -3] } # Creating DataFrame df = pd.DataFrame(d) # Display original DataFrame ...
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.Series.abs.html
pandas.Series.abs — pandas 2.3.3 documentation
For complex inputs, 1.2 + 1j, the absolute value is \(\sqrt{ a^2 + b^2 }\).
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 ?
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 ·