【发布时间】:2017-05-14 18:28:40
【问题描述】:
我使用的是 Python 2.7。标题提供上下文。我以这种特定的方式对标题进行了表述,以便人们将来可以查询此堆栈交换问题。使用 MATLAB 有很多关于这些东西的文档,但是 Scipy、NumPy、Pandas、matplotlib 等严重缺乏这个过程。
基本上,我有以下数据框:
time amplitude
0 1.0 0.1
1 2.0 -0.3
2 3.0 1.4
3 4.0 4.2
4 5.0 -5.7
5 6.0 2.3
6 7.0 -0.2
7 8.0 -0.3
8 9.0 1.0
9 10.0 0.1
现在我想做的是:
- 以 5 秒为间隔,查找最大值和最小值
- 用对应的时间值记录最大值和最小值(即对于上述情况,在前 5 秒内,最大值在 4 秒时为 4.2,在 5 秒时为 -5.7)
-
将值附加到数据框中的适当位置,即
time amplitude upper lower 0 1.0 0.1 1 2.0 -0.3 2 3.0 1.4 3 4.0 4.2 4.2 4 5.0 -5.7 -5.7 5 6.0 2.3 2.3 6 7.0 -0.8 -0.8 7 8.0 -0.3 8 9.0 1.0 9 10.0 0.1 在最大值和最小值之间进行插值以清除数据帧
绘制幅度列、上列和下列
我对 python/pandas 足够熟悉,想象一下代码如下所示:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import scipy as scipy
time = [0,1,2,3,4,5,6,7,8,9]
amplitude = [0.1,-0.3,1.4,4.2,-5.7,2.3,-0.2,-0.3,1.0,0.1]
df = pd.DataFrame({'time': time, 'amplitude': amplitude}]
plt.plot(df['time'],df['amplitude])
for seconds in time:
if <interval == 5>:
max = []
time_max = []
min = []
time_min = []
max.append(df.max['amplitude'])
min.append(df.min['amplitude'])
time_max.append(<time value in interval>)
time_min.append(<time value in interval>)
<build another dataframe>
<concat to existing dataframe df>
<interpolate between values in column 'upper'>
<interpolate between values in column 'lower'>
感谢任何帮助。
谢谢。
~德文
【问题讨论】:
-
第 6 行中的
amplitude值在示例数据的第一次和第二次显示中不同(-0.2与-0.8)。是哪个? -
需要什么样的插值协议?
-
具体情况无关紧要。顺便说一句,我回答了我自己的问题