Skip to content

Instantly share code, notes, and snippets.

@millzpaugh
Last active February 13, 2019 15:52
Show Gist options
  • Save millzpaugh/2e469327b2d13c1251005f8a53fd7fdb to your computer and use it in GitHub Desktop.
Save millzpaugh/2e469327b2d13c1251005f8a53fd7fdb to your computer and use it in GitHub Desktop.

Scriptz

Script 1: Returns new DF

import os
filepath = os.path.join(os.getcwd(), 'sample_wind_data.csv')
import pandas as pd 
df = pd.read_csv(filepath) 
df1 = df.head(50)

indices = []
index_bins = []
start = False 
for index, row in df1.iterrows():
    if row['AVGspeed'] > 8 and start == False:
        start = True
        index_bins.append(index)
    elif row['AVGspeed'] > 8 and start == True: 
        index_bins.append(index)
    else:
        if len(index_bins) >= 3: 
            indices += index_bins 
        start = False 
        index_bins = []

df2 = df1.loc[indices]

Example output

  • DF - drops rows that don’t meet > 8 speed + 3 consecutive hours criteria

Script 2: Returns new Series

import os
filepath = os.path.join(os.getcwd(), 'sample_wind_data.csv')
import pandas as pd 

df = pd.read_csv(filepath) 
df1 = df.head(50)

indices = []
index_bins = []
start = False 
for index, row in df1.iterrows():
    if row['AVGspeed'] > 8 and start == False:
        start = True
        index_bins.append((row['Date Time'], row['AVGspeed']))
    elif row['AVGspeed'] > 8 and start == True: 
        index_bins.append((row['Date Time'], row['AVGspeed']))
    else:
        if len(index_bins) >= 3: 
            indices.append(index_bins)
        start = False 
        index_bins = []

pd.Series(indices)

Example output

  • index, list of tuples for consecutive period (date, AVGspeed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment