我有一个看起来类似于
的数据框2020-01-07 09:00:00,22,228
2020-01-07 10:00:00,22,228
2020-01-07 11:00:00,22,228
2020-01-07 12:00:00,22,228
2020-01-07 13:00:00,22,228
2020-01-07 14:00:00,22,228
2020-01-07 15:00:00,21,228
2020-01-07 16:00:00,22,228
2020-01-08 09:00:00,43,45
2020-01-08 10:00:00,44,45
2020-01-08 11:00:00,41,45
2020-01-08 12:00:00,47,43
我想通过数据获得每列的第二个最小值。
我知道我可以通过执行df = df.resample('D', on='DateTime').min()
来获得最低要求,而且我知道nsmallest(n=2)
和last
可以结合起来以获取所需的东西。
如何将resample
与nsmallest
一起使用?
答案 0 :(得分:2)
将Resampler.aggregate
与自定义函数一起使用以返回一些值,例如NaN
(如果不存在第二个最小值):
def f(x):
v= x.nsmallest(2)
try:
return v.iat[1]
except:
return np.nan
df = df.resample('D', on='DateTime').agg(f)
如果要第二个唯一最小值:
def f(x):
v = np.sort(np.unique(x))
try:
return v[1]
except:
return np.nan