Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]
like image 543
Mainland Avatar asked Oct 29 '25 10:10

Mainland


1 Answers

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]
like image 150
BENY Avatar answered Nov 01 '25 01:11

BENY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!