简体   繁体   中英

Extracting hourly data from 15 minutes interval data in python pandas

I have a dataframe 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. I wish to add a new column and get the the summation of every 4 readings and thereby converting it to hourly data. For example for the '0'th hour it is (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

You can use 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

Use set_index and sum with level parameter:

(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 :

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

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