Assume, I have a data frame series containing increasing set of values and decreasing set of values. (Like a sawtooth pattern).
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
y = list(np.linspace(0, 1, 3)) list(np.linspace(1,0, 11)) * 5
x = list(range(len(y)))
df = pd.DataFrame({"x": x, "y":y})
df.plot("x", "y")
Now I would like to extract these down-sliding sections in to separate dfs. What would be the best way to do this ?
What I am expecting to see a list of dfs as below (image shows the data of the first df)
pd.DataFrame({"x": range(11), "y":list(np.linspace(1,0, 11))}).plot("x", "y")
CodePudding user response:
Use:
s = (df['y']-df['y'].shift(-1)>0)
t = s-s.shift(1)
u = t[t>=0].astype(int).cumsum()
u = u[u>0]
df.loc[u.index].groupby(u)['y'].apply(list)
Output"
y
1 [1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.3999999999999...
2 [1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.3999999999999...
3 [1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.3999999999999...
4 [1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.3999999999999...
5 [1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.3999999999999...
Name: y, dtype: object