简体   繁体   English

从python熊猫中的15分钟间隔数据中提取每小时数据

[英]Extracting hourly data from 15 minutes interval data in python pandas

I have a dataframe df: 我有一个数据框df:

Year Month  Day Hour Minute Reading
2011   1     1    0     0      1
2011   1     1    0    15     0.2
2011   1     1    0    30     0.4
2011   1     1    0    45     0.0
2011   1     1    1     0     0.2 
2011   1     1    1    15     0.5 
2011   1     1    1    30     0.3 
2011   1     1    1    45     0.1

The above dataframe has 15 minutes interval data. 上面的数据帧有15分钟间隔数据。 I wish to add a new column and get the the summation of every 4 readings and thereby converting it to hourly data. 我希望添加一个新列,并获取每4次读数的总和,从而将其转换为小时数据。 For example for the '0'th hour it is (1+0.2+0.4+0.0 = 1.6). 例如,第0个小时为(1 + 0.2 + 0.4 + 0.0 = 1.6)。

Hence my output should look like: 因此,我的输出应如下所示:

Year Month  Day Hour Minute Hourly_Reading
2011   1     1   0     0        1.6
2011   1     1   1     0        1.1

Can anyone please guide me with this? 有人可以指导我吗?

Option 1 选项1

You can use groupby : 您可以使用groupby

(df.groupby(['Year','Month','Day','Hour'])['Reading']
    .sum()
    .reset_index()
    .assign(Minutes=0)
    .reindex_axis(['Year','Month','Day','Hour','Minutes','Reading'],axis=1))

Output: 输出:

   Year  Month  Day  Hour  Minutes  Reading
0  2011      1    1     0        0      1.6
1  2011      1    1     1        0      1.1

Option 2 选项2

Use set_index and sum with level parameter: 使用set_index并使用level参数sum

(df.set_index(['Year','Month','Day','Hour'])['Reading']
    .sum(level=[0,1,2,3])
    .reset_index()
    .assign(Minutes=0)
    .reindex_axis(['Year','Month','Day','Hour','Minutes','Reading'],axis=1))

Output: 输出:

   Year  Month  Day  Hour  Minutes  Reading
0  2011      1    1     0        0      1.6
1  2011      1    1     1        0      1.1

If you wanted, you could also assign the result to df with transform : 如果需要,还可以使用transform将结果分配给df

df['Hourly_Reading'] = df.groupby(['Month', 'Hour'])['Reading'].transform('sum')

Result: 结果:

  Year  Month  Day  Hour  Minute  Reading  Hourly_Reading
0  2011      1    1     0       0      1.0             1.6
1  2011      1    1     0      15      0.2             1.6
2  2011      1    1     0      30      0.4             1.6
3  2011      1    1     0      45      0.0             1.6
4  2011      1    1     1       0      0.2             1.1
5  2011      1    1     1      15      0.5             1.1
6  2011      1    1     1      30      0.3             1.1
7  2011      1    1     1      45      0.1             1.1

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

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