簡體   English   中英

如何在 python 中重新采樣和插入新數據

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

我有一個包含以下信息的 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

我希望能夠將時間列更改為從 0 開始的 0.25 秒間隔,並讓相關的變量數據隨之更改(即,如果在 2.45 v=10 時,在 2.5 v=10.2 時)。 變量數據必須根據我假設的時間數據的變化進行插值嗎? 我需要能夠直接從 csv 執行此操作,而不是寫出 python 中的數據,因為真正的數據集是 1000 行。

不確定我想要的是否完全有可能,但有些想法會 go 一路走來,謝謝!

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:

    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