- API reference
- Series
- pandas.Series.abs
pandas.Series.abs#
- Series.abs()[source]#
Return a Series/DataFrame with absolute numeric value of each element.
This function only applies to elements that are all numeric.
- Returns:
- abs
Series/DataFrame containing the absolute value of each element.
See also
numpy.absolute
Calculate the absolute value element-wise.
Notes
For
complex
inputs,1.2+1j
, the absolute value is\(\sqrt{ a^2 + b^2 }\).Examples
Absolute numeric values in a Series.
>>>s=pd.Series([-1.10,2,-3.33,4])>>>s.abs()0 1.101 2.002 3.333 4.00dtype: float64
Absolute numeric values in a Series with complex numbers.
>>>s=pd.Series([1.2+1j])>>>s.abs()0 1.56205dtype: float64
Absolute numeric values in a Series with a Timedelta element.
>>>s=pd.Series([pd.Timedelta('1 days')])>>>s.abs()0 1 daysdtype: timedelta64[ns]
Select rows with data closest to certain value using argsort (fromStackOverflow).
>>>df=pd.DataFrame({...'a':[4,5,6,7],...'b':[10,20,30,40],...'c':[100,50,-30,-50]...})>>>df a b c0 4 10 1001 5 20 502 6 30 -303 7 40 -50>>>df.loc[(df.c-43).abs().argsort()] a b c1 5 20 500 4 10 1002 6 30 -303 7 40 -50
On this page