最初,我有一个Pandas DataFrame,它由A (for x-axis values)和B (for y-axis values)两列组成,它们被绘制成一个简单的x-y coordinate graph。数据由几个峰值组成,其中所有的峰值都发生在相同的y-axis值上,增量相同。因此,我能够做到以下几点:
df = pd.read_csv(r'/Users/_______/Desktop/Data Packets/Cycle Data.csv')
nrows = int(df['B'].max() * 2) - 1
alphabet: list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
groups = df.groupby(df.index // nrows)
for (frameno, frame) in groups:
frame.to_csv("/Users/_______/Desktop/Cycle Test/" + alphabet[frameno] + "%s.csv" % frameno, index=False)上述代码将大周期数据文件解析为多个相同大小的数据文件,因为每个循环的局部最小值和最大值是相同的。
但是,我希望能够解析具有任意峰值和最小值的数据文件。我不能同时拆分大数据文件,因为每个数据文件都有不同的大小。下面是一个例子:

编辑:示例数据(A为x轴,B为y轴):
data = {'A': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26], 'B': [0, 1, 2, 3, 4, 5, 6, 7, 5, 3, 1, -1, 1, 3, 5, 7, 9, 8, 7, 6, 5, 4, 6, 8, 6, 4, 2]}
df = pd.DataFrame(data)编辑2:不同的样本数据(Displacement从1到50返回1,然后1到60返回1,等等):
Load Displacement
0 0.100000 1.0
1 0.101000 2.0
2 0.102000 3.0
3 0.103000 4.0
4 0.104000 5.0
.. ... ...
391 0.000006 5.0
392 0.000005 4.0
393 0.000004 3.0
394 0.000003 2.0
395 0.000002 1.0发布于 2020-08-16 19:21:52
col = df['B'] # replace with the appropriate column name
# find local minima. FIXED: use rightmost min value if repeating
minima = (col <= col.shift()) & (col < col.shift(-1))
# create groups
groups = minima.cumsum()
# group
df.groupby(groups).whatever() # replace with whatever the appropriate aggregation is例如,计数值:
df.groupby(groups).count()
Out[10]:
A B
B
0 11 11
1 10 10
2 6 6发布于 2020-08-16 19:20:18
我们可以试试scipy,argrelextrema
from scipy.signal import argrelextrema
idx = argrelextrema(df.col.values, np.less)
g = df.groupby(df.index.isin(df.index[idx[0]]).cumsum())https://stackoverflow.com/questions/63441007
复制相似问题