在条件中找到DataFrame.index中的最小值

时间:2015-08-12 18:49:04

标签: python pandas

有两个DataFrames df1和df2都有时间戳作为索引,我试图在条件下找到df2到df1的最近索引。

reference_index = df1.index[0]
index_differences = reference_index - df2.index
cond1 = index_diff >= timedelta(hours=0)
cond2 = index_diff <= timedelta(hours=1)
combined_cond = cond1 & cond2

如何在条件combined_cond下找到index_differences中的最小值?

2 个答案:

答案 0 :(得分:1)

x = Series([1, 2, 3, 4 ,5, 6])
y = Series([False, False, True, True, False, False])
x[y].argmin()

答案 1 :(得分:0)

x = [1, 2, 3, 4 ,5, 6]
y = [False, False, True, True, False, False]


print x.index(min([i for i in x if y[x.index(i)]]))

你可以试试这个。