简体   繁体   English

如何在 python 中重新采样和插入新数据

[英]How to re-sample and interpolate new data in python

I have a csv file containing the following information:我有一个包含以下信息的 csv 文件:

Time(s) Variable
0.003   1
0.009   2
0.056   3
0.094   4
0.4     5
0.98    6
1.08    7
1.45    8
1.89    9
2.45    10
2.73    11
3.2     12
3.29    13
3.5     14

I would like to be able to be able to change the time column into 0.25s intervals starting from 0, and have the associated variable data change along with it (ie if at 2.45 v=10, at 2.5 v=10.2).我希望能够将时间列更改为从 0 开始的 0.25 秒间隔,并让相关的变量数据随之更改(即,如果在 2.45 v=10 时,在 2.5 v=10.2 时)。 The variable data would have to be interpolated against the change in the time data I assume?变量数据必须根据我假设的时间数据的变化进行插值吗? I need to be able to do this straight from csv rather than writing out the data in python as the real data-set is 1000's of rows.我需要能够直接从 csv 执行此操作,而不是写出 python 中的数据,因为真正的数据集是 1000 行。

Not sure if what I want is exactly possible but some thoughts would go along way, thanks!不确定我想要的是否完全有可能,但有些想法会 go 一路走来,谢谢!

How about Scipy's interp1d Scipy的interp1d怎么样

from scipy.interpolate import interp1d

interp = interp1d(df['Time(s)'], df['Variable'])

new_times = np.arange(0.25, 3.5, 0.25)
pd.DataFrame({'Time(s)': new_times, 'Variable':interp(new_times)})

Output: Output:

    Time(s)   Variable
0      0.25   4.509804
1      0.50   5.172414
2      0.75   5.603448
3      1.00   6.200000
4      1.25   7.459459
5      1.50   8.113636
6      1.75   8.681818
7      2.00   9.196429
8      2.25   9.642857
9      2.50  10.178571
10     2.75  11.042553
11     3.00  11.574468
12     3.25  12.555556

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM