• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Jun 18th, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. df['min'] = df.data[(df.data.shift(1) > df.data) & (df.data.shift(-1) > df.data)]
2. df['max'] = df.data[(df.data.shift(1) < df.data) & (df.data.shift(-1) < df.data)]
3.
4. n = 3
5. # Store all peaks in a series
6. min_vals = df.data[(df.data.shift(1) > df.data) & (df.data.shift(-1) > df.data)]
7. max_vals = df.data[(df.data.shift(1) < df.data) & (df.data.shift(-1) < df.data)]
8.
9. # Iterate over all values in my dataframe
10. for idx, row in df.iterrows():
11.
12.     # get all peaks that appeared before the current row (avoid look ahead)
13.     tmp_min = min_vals.loc[(idx >= min_vals.index)]
14.     tmp_max = max_vals.loc[(idx >= max_vals.index)]
15.
16.     # Test if at least n mins and max peaks already appeared
17.     if len(tmp_min) >= n and len(tmp_max) >= n:
18.
19.          #create counter for min values (needed to create column name)
20.          min_ctr = 0
21.
22.          # iterate over last n entries in tmp_min by using tail function
23.          for x in tmp_min.tail(n):
24.              df.loc[idx, 'min_' + str(min_ctr)] = row.data
25.              min_ctr += 1
26.
27.          max_ctr = 0
28.          for x in tmp_min.tail(n):
29.              df.loc[idx, 'max_' + str(max_ctr)] = row.data
30.              max_ctr += 1
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top