在 Pandas 数据框中获得几年内工作日某个小时的平均值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16967165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the average of a certain hour on weekdays over several years in a pandas dataframe
提问by Markus W
I have an hourly dataframe in the following format over several years:
几年来,我有以下格式的每小时数据框:
Date/Time Value
01.03.2010 00:00:00 60
01.03.2010 01:00:00 50
01.03.2010 02:00:00 52
01.03.2010 03:00:00 49
.
.
.
31.12.2013 23:00:00 77
I would like to average the data so I can get the average of hour 0, hour 1... hour 23 of each of the years.
我想平均数据,这样我就可以得到每年第 0 小时、第 1 小时……第 23 小时的平均值。
So the output should look somehow like this:
所以输出应该看起来像这样:
Year Hour Avg
2010 00 63
2010 01 55
2010 02 50
.
.
.
2013 22 71
2013 23 80
Does anyone know how to obtain this in pandas?
有谁知道如何在Pandas中获得这个?
回答by Andy Hayden
Note: Now that Series have the dt accessor it's less important that date is the index, though Date/Time still needs to be a datetime64.
注意:既然 Series 有 dt 访问器,日期是索引就不那么重要了,尽管日期/时间仍然需要是 datetime64。
Update: You can do the groupby more directly (without the lambda):
更新:您可以更直接地进行 groupby(没有 lambda):
In [21]: df.groupby([df["Date/Time"].dt.year, df["Date/Time"].dt.hour]).mean()
Out[21]:
Value
Date/Time Date/Time
2010 0 60
1 50
2 52
3 49
In [22]: res = df.groupby([df["Date/Time"].dt.year, df["Date/Time"].dt.hour]).mean()
In [23]: res.index.names = ["year", "hour"]
In [24]: res
Out[24]:
Value
year hour
2010 0 60
1 50
2 52
3 49
If it's a datetime64 indexyou can do:
如果它是 datetime64索引,您可以执行以下操作:
In [31]: df1.groupby([df1.index.year, df1.index.hour]).mean()
Out[31]:
Value
2010 0 60
1 50
2 52
3 49
Old answer (will be slower):
旧答案(会更慢):
Assuming Date/Time was the index* you can use a mapping function in the groupby:
假设日期/时间是索引*,您可以在groupby 中使用映射函数:
In [11]: year_hour_means = df1.groupby(lambda x: (x.year, x.hour)).mean()
In [12]: year_hour_means
Out[12]:
Value
(2010, 0) 60
(2010, 1) 50
(2010, 2) 52
(2010, 3) 49
For a more useful index, you could then create a MultiIndex from the tuples:
对于更有用的索引,您可以从元组创建一个 MultiIndex:
In [13]: year_hour_means.index = pd.MultiIndex.from_tuples(year_hour_means.index,
names=['year', 'hour'])
In [14]: year_hour_means
Out[14]:
Value
year hour
2010 0 60
1 50
2 52
3 49
* if not, then first use set_index:
* 如果没有,则首先使用set_index:
df1 = df.set_index('Date/Time')
回答by enmyj
If your date/time column were in the datetime format (see dateutil.parser for automatic parsing options), you can use pandas resample as below:
如果您的日期/时间列采用日期时间格式(有关自动解析选项,请参阅 dateutil.parser),您可以使用如下所示的 pandas resample:
year_hour_means = df.resample('H',how = 'mean')
which will keep your data in the datetime format. This may help you with whatever you are going to be doing with your data down the line.
这将使您的数据保持日期时间格式。这可能会帮助您处理您将要处理的数据。

