将Pandas Dataframe转换为嵌套的json

时间:2017-11-03 09:38:49

标签: python json pandas

我需要将pandas Dataframe转换为嵌套的json。 .to_json的输出给出以下

{"Annual Expenditure":{"0":250,"1":250},"Annual Frequency":{"0":1,"1":1},"Avg days":{"0":null,"1":null},"First visit":{"0":1449100800000,"1":1490054400000},"Frequency":{"0":1,"1":1},"Guest":{"0":25642,"1":55521},"Last visit":{"0":1449100800000,"1":1490054400000},"Monetory":{"0":250,"1":250},"Recency":{"0":701,"1":227},"Visit_Ids":{"0":[80611],"1":[342104]},"RFMClass":{"0":"444","1":"144"},"AvgLTV":{"0":13305.7692307692,"1":13305.7692307692}}

但是我需要在一个嵌套的json中使用key作为guest_id并且有相应的值。像这样:

{55521: {'Monetory': 250, 'First visit': datetime.date(2017, 3, 21), 'Annual Expenditure': 250, 'Frequency': 1, 'Last visit': datetime.date(2017, 3, 21), 'Avg days': NaT, 'Annual Frequency': 1, 'Recency': 227, 'Visit_Ids': [342104]}, 25642: {'Monetory': 250, 'First visit': datetime.date(2015, 12, 3), 'Annual Expenditure': 250, 'Frequency': 1, 'Last visit': datetime.date(2015, 12, 3), 'Avg days': NaT, 'Annual Frequency': 1, 'Recency': 701, 'Visit_Ids': [80611]}}

.to_json()函数不能按照我想要的方式工作,而且我已经用尽了所有选项。任何想法如何进行?

编辑:

谢谢你的回答!我将json输出作为循环的一部分,我得到的每个输出都是一个独特的JSON。那么有没有办法创建以下内容?:

{45: {"50492":{"Annual Expenditure":1000,"Annual Frequency":1,"Avg days":null,"First visit":1486339200000,"Frequency":1,"Last visit":1486339200000,"Merchants":45,"Monetory":1000,"Recency":270,"Visit_Ids":[300758],"RFMClass":"144","AvgLTV":113800.0},"1041":{"Annual Expenditure":1000,"Annual Frequency":1,"Avg days":null,"First visit":1478649600000,"Frequency":1,"Last visit":1478649600000,"Merchants":45,"Monetory":1000,"Recency":359,"Visit_Ids":[257022],"RFMClass":"244","AvgLTV":113800.0},"783":{"Annual Expenditure":1000,"Annual Frequency":1,"Avg days":null,"First visit":1457049600000,"Frequency":1,"Last visit":1457049600000,"Merchants":45,"Monetory":1000,"Recency":609,"Visit_Ids":[123464],"RFMClass":"444","AvgLTV":113800.0}}}

这里45是一个唯一的标识符,我将在循环结束时传入。

1 个答案:

答案 0 :(得分:2)

使用set_index + to_json参数orient

df.set_index('Guest').to_json('file.json', orient='index')
{
    "25642": {
        "Annual Expenditure": 250,
        "Annual Frequency": 1,
        "Avg days": null,
        "AvgLTV": 13305.7692307692,
        "First visit": 1449100800000,
        "Frequency": 1,
        "Last visit": 1449100800000,
        "Monetory": 250,
        "RFMClass": "444",
        "Recency": 701,
        "Visit_Ids": [80611]
    },
    "55521": {
        "Annual Expenditure": 250,
        "Annual Frequency": 1,
        "Avg days": null,
        "AvgLTV": 13305.7692307692,
        "First visit": 1490054400000,
        "Frequency": 1,
        "Last visit": 1490054400000,
        "Monetory": 250,
        "RFMClass": "144",
        "Recency": 227,
        "Visit_Ids": [342104]
    }
}

输入DataFrame

d = {"Annual Expenditure":{"0":250,"1":250},"Annual Frequency":{"0":1,"1":1},"Avg days":{"0":np.nan,"1":np.nan},"First visit":{"0":1449100800000,"1":1490054400000},"Frequency":{"0":1,"1":1},"Guest":{"0":25642,"1":55521},"Last visit":{"0":1449100800000,"1":1490054400000},"Monetory":{"0":250,"1":250},"Recency":{"0":701,"1":227},"Visit_Ids":{"0":[80611],"1":[342104]},"RFMClass":{"0":"444","1":"144"},"AvgLTV":{"0":13305.7692307692,"1":13305.7692307692}}
df = pd.DataFrame(d)
print (df)
   Annual Expenditure  Annual Frequency  Avg days        AvgLTV  \
0                 250                 1       NaN  13305.769231   
1                 250                 1       NaN  13305.769231   

     First visit  Frequency  Guest     Last visit  Monetory RFMClass  Recency  \
0  1449100800000          1  25642  1449100800000       250      444      701   
1  1490054400000          1  55521  1490054400000       250      144      227   

  Visit_Ids  
0   [80611]  
1  [342104] 

编辑:

j = df.set_index('Guest').to_json(orient='index')
j_final = {45: j}