【问题标题】:Pandas: Calculate column mean values for hourly dataPandas:计算每小时数据的列平均值
【发布时间】:2020-04-29 15:22:13
【问题描述】:

我有一个像这样的 Pandas 数据框('Timestamp' 在 datetime type 和索引 col.):

                                    Server Citta Nazione  download Mb/s  upload Mb/s     ping            Isp
Timestamp                                                                                                   
2020-04-01 11:02:04        AlternatYva srl  Rome   Italy      12.550000     0.890000   70.918  Warian S.R.L.
2020-04-01 11:04:12        AlternatYva srl  Rome   Italy      10.880000     0.510000   64.908  Warian S.R.L.
2020-04-01 11:06:07            Fastweb SpA  Rome   Italy      11.200000     0.650000   63.223  Warian S.R.L.
2020-03-23 05:00:13            Fastweb SpA  Rome   Italy      13.956026     0.629037   31.809  Warian S.R.L.
2020-03-23 05:02:08        AlternatYva srl  Rome   Italy      10.887535     0.224637   31.200  Warian S.R.L.
...                                    ...   ...     ...            ...          ...      ...            ...
2020-04-07 09:03:37        AlternatYva srl  Rome   Italy      12.560000     1.030000   55.119  Warian S.R.L.
2020-04-07 09:05:12            Fastweb SpA  Rome   Italy      13.640000     0.770000   29.715  Warian S.R.L.
2020-04-25 02:01:52        AlternatYva srl  Rome   Italy      10.990000     0.040000   74.318  Warian S.R.L.
2020-04-25 02:03:28  Telecom Italia S.p.A.  Rome   Italy      11.510000     1.090000  137.830  Warian S.R.L.
2020-04-25 02:04:56  Telecom Italia S.p.A.  Rome   Italy      12.960000     0.330000   65.324  Warian S.R.L.

[6726 rows x 7 columns]

我想使用每小时“下载 Mb/s”列的下载平均值创建一个新的 df。两列 HOUR - 平均值。比如:

HOUR   mean
0       12.
1       13.5
2        4.8
3        9.6
...
23      10.2

到目前为止,我可以通过 mean() 函数计算主数据帧的“下载 Mb/s”列的值。而且我知道,使用between_time() 函数,我可以选择索引列'Timestamp' 小时之间的所有行。

将这两个函数聚合在一起以获得上述数据帧的正确方法是什么?

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    你可以分组:

    df.groupby(df.index.hour)[['download Mb/s', 'upload Mb/s']].mean()
    

    【讨论】:

    • df.groupby(df.index.hour) 是我要找的 ;-)
    • 我认为如果这个答案对您有所帮助,那么您应该将此答案标记为已接受,因为这将对其他人更有帮助。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2023-03-06
    • 2021-05-09
    • 2019-02-23
    • 2016-02-19
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多