Mainland
Mainland

Reputation: 4584

Python detect zero crossing in a dataframe

I have a data frame. I want to detect the zero crossing a dataframe:

My code:

def zero_crossing(data):
    return np.where(np.diff(np.sign(np.array(data))))[0]

df1 = pd.DataFrame({'a':[1,2,-1,5,0,9,-6,7]},index=[100,101,102,103,104,105,106,107])

df1 = 
     a
100  1
101  2
102 -1
103  5
104  0
105  9
106 -6
107  7
print(zero_crossing(df1['a']))

Present output:

array([1, 2, 3, 4, 5, 6], dtype=int64)

Expected output:

[101,102,103,104,105,106]

Upvotes: 4

Views: 3024

Answers (1)

BENY
BENY

Reputation: 323326

Use your result

df1.index[zero_crossing(df1['a'])]
Out[132]: Int64Index([101, 102, 103, 104, 105, 106], dtype='int64')

#df1.index[zero_crossing(df1['a'])].tolist()
#Out[133]: [101, 102, 103, 104, 105, 106]

Upvotes: 4

Related Questions