I have a pandas dataframe and a following graph based on a column of the dataframe: Image here
How can I best split the data based on the red line I've marked? (Apologize if the image is not really clear)
I tried to get the maximum/minimum value for the graph but it only shows one instead of multiple maximum values in the graph.
I'm currently practicing how to use pandas dataframe and indexing and any help on how to approach this would be really appreciated, thanks!
CodePudding user response:
The answer from Foad S. Farimani will solve your problem I think.
Rewriting for your example, the local minima will be used.
# import packages
import numpy as np
import pandas as pd
from scipy.signal import argrelmin
# example data
x = list(np.arange(-100, 100, 0.5))
y = np.sin(x)
df = pd.DataFrame({"y": y}, index=x)
# get the index values of the local minima
minima = df.iloc[argrelmin(df["y"].values)].index
# dictionary for the splits
splits = {}
# split the dataframe into smaller dataframes between the local minima
for n, (i, j) in enumerate(zip([df.index[0]] list(minima), list(minima) [df.index[-1]])):
splits[f"df_{n}"] = df.loc[i:j, :]
Plotting the data will return different colours for each 'section' of the original dataframe
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
for i in splits.keys():
ax.plot(splits[i].index, splits[i]["y"])
plt.show()