简体   繁体   中英

How to re-sample and interpolate new data in python

I have a csv file containing the following information:

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). 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.

Not sure if what I want is exactly possible but some thoughts would go along way, thanks!

How about Scipy's 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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