简体   繁体   English

熊猫重新采样数据框

[英]Pandas resample dataframe

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. 我想使“时间”列成为索引,并将数据帧降采样为1s。 Help please? 请帮助?

You can use reindex and choose one fill method 您可以使用reindex并选择一种填充方法

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): 然后每秒进行resample (默认情况下为平均值,但可以将其更改为sum等-例如将how='sum'添加为参数):

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. 如果重要,可以更改年份/日期。

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

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