Examples

Absolute numeric values in a Series.

In [1]:
import numpy as np
import pandas as pd
In [2]:
s = pd.Series([-2.8, 3, -4.44, 5])
s.abs()
Out[2]:
0    2.80
1    3.00
2    4.44
3    5.00
dtype: float64

Absolute numeric values in a Series with complex numbers.

In [3]:
s = pd.Series([2.2 + 1j])
s.abs()
Out[3]:
0    2.416609
dtype: float64

Absolute numeric values in a Series with a Timedelta element.

In [4]:
s = pd.Series([pd.Timedelta('2 days')])
s.abs()
Out[4]:
0   2 days
dtype: timedelta64[ns]

Select rows with data closest to certain value using argsort.

In [5]:
df = pd.DataFrame({
    'p': [2, 3, 4, 5],
    'q': [10, 20, 30, 40],
    'r': [200, 60, -40, -60]
})
df
Out[5]:
p q r
0 2 10 200
1 3 20 60
2 4 30 -40
3 5 40 -60
In [11]:
df.loc[(df.r - 45).abs().argsort()]
Out[11]:
p q r
1 3 20 60
2 4 30 -40
3 5 40 -60
0 2 10 200