I have a resampling (downsampling) problem that should be straightforward to do but I'm not able!! Here is a simplified example:
df:
Time A
0 0.01591 0.108929
1 0.27973 0.411764
2 0.55044 0.064253
3 0.81386 0.317394
4 1.07983 0.722707
5 1.35051 1.154193
6 1.61495 1.151492
7 1.88035 0.123389
8 2.15462 0.093583
9 2.41534 0.260944
10 2.67992 1.007564
11 2.95148 0.325353
12 3.21364 0.555593
13 3.47980 0.740621
15 4.01519 1.619669
16 4.28679 0.477371
17 4.55482 0.432049
18 4.81570 0.194224
19 5.07992 0.331936
The Time column is in seconds. I would like to make the Time column the index and downsample the dataframe to 1s. Help please?
You can use reindex
and choose one fill method
In [37]: df.set_index('Time').reindex(range(0,6), method='bfill')
Out[37]:
A
0 0.108929
1 0.722707
2 0.093583
3 0.555593
4 1.619669
5 0.331936
First convert your index to datetime format:
df.index=pd.to_datetime(df.Time,unit='s')
Then resample
by second (this is the mean value by default but can be changed to sum etc - eg add how='sum'
as parameter):
d.resample('S')
Time A
Time
1970-01-01 00:00:00 0.414985 0.225585
1970-01-01 00:00:01 1.481410 0.787945
1970-01-01 00:00:02 2.550340 0.421861
1970-01-01 00:00:03 3.346720 0.648107
1970-01-01 00:00:04 4.418125 0.680828
1970-01-01 00:00:05 5.079920 0.331936
The year/date can be changed if important.
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.