【问题标题】:Representing pandas describe() in dict form以字典形式表示 pandas describe()
【发布时间】:2017-05-08 10:14:11
【问题描述】:

我的时间序列数据看起来像这样:

>>> data

            cost             Timestamp   value
0     0.0032 2016-10-01 00:00:00-04:00  0.0179
1     0.0033 2016-10-01 01:00:00-04:00  0.0181
2     0.0741 2016-10-01 02:00:00-04:00  0.4117
3     0.0679 2016-10-01 03:00:00-04:00  0.3769
4     0.0761 2016-10-01 04:00:00-04:00  0.4230
5     0.0868 2016-10-01 05:00:00-04:00  0.4823
...

我希望能够以 dict 形式表示以下按值分组的摘要,以便可以通过 RESTful 框架返回

>>> times = pd.DatetimeIndex(data['Timestamp'])
>>> data.groupby(times.time).describe()

                     cost      value
00:00:00 count  43.000000  43.000000
         mean    0.004323   0.024060
         std     0.003811   0.021196
         min     0.003200   0.017500
         25%     0.003200   0.017800
         50%     0.003200   0.017900
         75%     0.003200   0.018000
         max     0.023100   0.128300
01:00:00 count  44.000000  44.000000
         mean    0.010641   0.059143
         std     0.015058   0.083642
         min     0.003200   0.017500
         25%     0.003200   0.017800
         50%     0.003200   0.018000
         75%     0.011600   0.064400
         max     0.058300   0.323700
...
23:00:00 count  44.000000  44.000000
         mean    0.028773   0.159902
         std     0.003627   0.020182
         min     0.022900   0.127500
         25%     0.025600   0.142500
         50%     0.029350   0.162850
         75%     0.031575   0.175200
         max     0.036100   0.200300

我希望输出看起来像这样

{ 
    summary: [
        {time: 00:00:00, 
         cost: {count: 43,
                mean: 0.04323
                std: ...
                ...
                max: 0.0231}},
         value: {count: 43,
                mean: 0.02406
                std: ...
                ...
                max: 0.12830}
        },
        {time: 01:00:00,
         cost: {...},
         value: {...},
        },
        ...,
        {time: 23:00:00,
         cost: {...},
         value: {...},
        }
    ]
} 

我已经测试了 pandas to_dict() 函数中的样式,但没有一个完全得到我想要的结果

【问题讨论】:

    标签: python pandas dictionary group-by


    【解决方案1】:

    我能够使用一些数据透视表魔术将其转换为我想要的格式

    def day_summary(data):
      times = pd.DatetimeIndex(data['Timestamp'])
      day_average = data.groupby(times.time).describe().reset_index().rename(columns={'level_0': 'time'})
      value_summary = day_average.pivot(index='time', columns='level_1', values='value').to_dict('records')
      cost_summary = day_average.pivot(index='time', columns='level_1', values='cost').to_dict('records')
      summary_times = day_average.pivot(index='time', columns='level_1', values='level_1').index
    
      composed_summary = [{'time': t, 'value': x, 'cost': y} for x, y, t in zip(value_summary, cost_summary, summary_times)]
      return composed_summary
    

    【讨论】:

      猜你喜欢
      • 2021-11-11
      • 1970-01-01
      • 2010-11-02
      • 2019-09-29
      • 2018-07-04
      相关资源
      最近更新 更多