Skip to content Skip to sidebar Skip to footer

How To Find Patterns In Pandas?

Using pandas and python, I want to find a pattern where a stream's inflow is much larger than usual, and it is followed within 5 days with an outflow that is no less than 5% of the

Solution 1:

Try doing a rolling averaging on df['stream'].

stream = [2, 0, 1, 0, 3, 2, 100, 0, 0, -95, 3, 0, 2, -1, 0, 2, 93, -2, -89]
date = [
    '2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04', '2019-01-05',
    '2019-01-06', '2019-01-07', '2019-01-08', '2019-01-09', '2019-01-10',
    '2019-01-11', '2019-01-13', '2019-01-14', '2019-01-15', '2019-01-16',
    '2019-01-17', '2019-01-18', '2019-01-19', '2019-01-20'
]

df = pd.DataFrame({'date': date, 'stream': stream})

def process(row):
    if row['stream'] > 20*row['stream_mean']:
        return 1
    else:
        return 0
df['stream_mean'] = df['stream'].rolling(5).mean()
df['stream_mean'] = df['stream_mean'].shift(periods=1)
df['flag'] = df.apply(process,axis=1)
df

It would be better if you apply Bollinger Band and create a Standard Deviation column and may be also try a 95% Confidence interval method.

Hope it helps :)


Post a Comment for "How To Find Patterns In Pandas?"