Linminxiang
Linminxiang

Reputation: 337

Using python to create an average out of a list of times in pandas

I have a large number data.

link

I need to average each fifteen minutes 'w'.

link

Now I use for loop to execute,but it is so slow.

pandas have any suite can help?

I really need your help.Many thanks.

Upvotes: 1

Views: 67

Answers (1)

jezrael
jezrael

Reputation: 862481

There are 2 possible different solutions - resampling by 15Min and aggregate columns by mean and first value:

df = df.resample('15T', on='reporttime').agg({'w':'mean', 'buildingid':'first'})

Or groupbing by column buildingid with Grouper for resampling:

df = df.groupby(['buildingid', pd.Grouper(key='reporttime',freq='15T')])['w'].mean()

Sample:

rng = pd.date_range('2017-04-03 18:09:04', periods=10, freq='7T')
df = pd.DataFrame({'reporttime': rng, 'w': range(10), 'buildingid':[39] * 5 + [40] * 5})  
print (df)
           reporttime  w  buildingid
0 2017-04-03 18:09:04  0          39
1 2017-04-03 18:16:04  1          39
2 2017-04-03 18:23:04  2          39
3 2017-04-03 18:30:04  3          39
4 2017-04-03 18:37:04  4          39
5 2017-04-03 18:44:04  5          40
6 2017-04-03 18:51:04  6          40
7 2017-04-03 18:58:04  7          40
8 2017-04-03 19:05:04  8          40
9 2017-04-03 19:12:04  9          40

df1 = df.resample('15T', on='reporttime').agg({'w':'mean', 'buildingid':'first'}).reset_index()
print (df1)
           reporttime    w  buildingid
0 2017-04-03 18:00:00  0.0          39
1 2017-04-03 18:15:00  1.5          39
2 2017-04-03 18:30:00  4.0          39
3 2017-04-03 18:45:00  6.5          40
4 2017-04-03 19:00:00  8.5          40

df2 = df.groupby(['buildingid', pd.Grouper(key='reporttime',freq='15T')])['w'].mean().reset_index()
print (df2)
   buildingid          reporttime    w
0          39 2017-04-03 18:00:00  0.0
1          39 2017-04-03 18:15:00  1.5
2          39 2017-04-03 18:30:00  3.5
3          40 2017-04-03 18:30:00  5.0
4          40 2017-04-03 18:45:00  6.5
5          40 2017-04-03 19:00:00  8.5

Upvotes: 1

Related Questions