data = dict( a= 0.2, b= 0.3, c= -1.5, d= -1.3, e= 0.3, f= 0.2, g= 12, h= -1.2, i= -1.3, j= 1.4, k= 0.3)
neg = True
for (k,v) in data.items():
if((neg and v<-1) or (not neg and v>1)):
print(k)
neg = not neg
возвращает:
c
g
h
j
Логика та же, что и у dict или dataframe, но убедитесь, что ваши ключи отсортированы:
(pi6 637) $ cat /tmp/x.py
#! /usr/bin/env python
# -*- coding: UTF8 -*-
import pandas as pd
data = dict( a= 0.2, b= 0.3, c= -1.5, d= -1.3, e= 0.3, f= 0.2, g= 12, h= -1.2, i= -1.3, j= 1.4, k= 0.3)
df = pd.Series(data)
neg = True
for i,r in df.sort_index().iteritems():
if((neg and r<-1) or (not neg and r>1)):
print(i)
neg = not neg
(pi6 638) $ /tmp/x.py
c
g
h
j