如何从熊猫系列获得绝对值较小的值

时间:2019-09-01 12:02:05

标签: python python-3.x pandas series

如何从熊猫系列中获得绝对值较小的值?

示例代码:

import pandas as pd

def get_min_absvalue(values):
    ¿?

lst = [-2.1,2.2,-1.5]

ser = pd.Series(lst)
min_absvalue = get_min_absvalue(ser)
#min_absvalue must be -1.5 here

1 个答案:

答案 0 :(得分:3)

您可以将minabs一起用作主要功能:

import pandas as pd

def get_min_absvalue(values):
    return min(values, key = abs)

lst = [-2.1, 2.2, -1.5]

ser = pd.Series(lst)
min_absvalue = get_min_absvalue(ser)

print(min_absvalue)

输出:

-1.5