我有如下所示的数据框对象,
index Date Poly_1 Poly_2 Poly_2_WLS Poly_3
0 0 2017-01-04 08:45:00 70.195597 83.613845 83.613845 99.041125
1 1 2017-01-04 08:53:00 70.195597 83.613845 83.613845 99.041125
2 2 2017-01-04 09:00:00 70.195597 83.613845 83.613845 99.041125
3 3 2017-12-13 08:45:00 70.195597 83.613845 83.613845 99.041125
4 4 2017-12-13 08:53:00 70.195597 83.613845 83.613845 99.041125
我正在使用以下代码按月对以上数据进行分组,
dfgrp=df.groupby(pd.Grouper(key='Date',freq="M"),as_index=False)
稍后我想使用以下代码将分组数据转换为json格式;
dfgrp.to_json(date_format='iso',orient='records')
但是由于某种原因,我遇到了错误
AttributeError:无法访问“ DataFrameGroupBy”对象的可调用属性“ to_json”,请尝试使用“ apply”方法
请告诉我,如何将以上数据帧转换为json?
编辑:
我从下面的答案中使用了下面的代码行;并能够生成JSON。
df.groupby([df.Date.dt.month)])['Poly_1','Poly_2','Poly_2_WLS','Poly_3'].sum().reset_index().to_json()
但是JSON以这种格式生成
`{"Date":{"0":1,"1":2,"2":3,"3":4,"4":5,"5":9,"6":10,"7":11,"8":12},"Poly_1":{"0":46187.2636499188,"1":56636.9594359758,"2":53218.6089763865,"3":41100.9574106447,"4":49317.907305443,"5":2670.6255284702,"6":34887.4415455112,"7":45857.8601621408,"8":21635.3343188418},"Poly_2":{"0":46193.719351124,"1":56193.0159455145,"2":52890.1916931438,"3":41119.1740551722,"4":49648.1531559606,"5":2767.3530477022,"6":34704.8815525262,"7":45918.9353954344,"8":22077.5341367508},"Poly_2_WLS":{"0":46193.719351124,"1":56193.0159455145,"2":52890.1916931438,"3":41119.1740551722,"4":49648.1531559606,"5":2767.3530477022,"6":34704.8815525262,"7":45918.9353954344,"8":22077.5341367508},"Poly_3":{"0":46037.6280724075,"1":56111.2211081627,"2":53059.8469394733,"3":41282.9093221716,"4":49670.016727901,"5":2660.8721082338,"6":34724.1756869611,"7":45721.7694774285,"8":22244.5188905397}`}
但是,我想要JSON,就像下面的格式一样;
预期的示例Json格式:
{
,
"Poly_1": {
"Jan": 46187.2636499188,
"Feb": 56636.9594359758,
"Mar": 53218.6089763865,
"Apr": 41100.9574106447,
"May": 49317.907305443,
"Jun": 2670.6255284702,
"July": 34887.4415455112,
"Aug": 45857.8601621408,
"Sept": 21635.3343188418
}
}
请建议我如何获得高于预期格式的格式?
谢谢
答案 0 :(得分:1)
我在回答中假设您希望对所有多边形进行求和(否则,只需更改下面的代码)。
我将以这种方式对其进行分组,以将7月设为(7):
df.groupby([df.Date.dt.month)])['Poly_1','Poly_2','Poly_2_WLS','Poly_3'].sum().reset_index().to_json()
注意:我添加了sum,reset_index,然后才添加到to_json。
您分组的df如下所示:
Date Poly_1 Poly_2 Poly_2_WLS Poly_3
0 7 350.977985 418.069225 418.069225 495.205625
您的json将是(如果您不重置索引)。
'{"Poly_1":{"7":350.977985},"Poly_2":{"7":418.069225},"Poly_2_WLS":{"7":418.069225},"Poly_3":{"7":495.205625}}'
另一个指针:如果您和石斑鱼一起去,您将拥有2019-07-31,并且您的json看起来像这样(1564531200000)
df.groupby([pd.Grouper(key='Date',freq="M")])['Poly_1','Poly_2','Poly_2_WLS','Poly_3'].sum().to_json()
'{"Poly_1":{"1564531200000":350.977985},"Poly_2":{"1564531200000":418.069225},"Poly_2_WLS":{"1564531200000":418.069225},"Poly_3":{"1564531200000":495.205625}}'
因此请调整您的需求。