gpt4 book ai didi

python - 将 json dict 加载到数据框中

转载 作者:太空宇宙 更新时间:2023-11-04 00:32:15 25 4
gpt4 key购买 nike

我有一个像这样格式化的 json 字典

{"cache_age_milliseconds": 0, "rows": [{"values": [["Sonos_HXXpu71TY1g4HWWU2jXCJ8tcKu", 1483225200000, "87.61.241.100", "*null*"], 0.3605555555555556]}, {"values": [["Sonos_HXXpu71TY1g4HWWU2jXCJ8tcKu", 1483221600000, "87.61.241.100", "*null*"], 0.35888888888888887]}], "columns": [{"type": "array", "label": ["Household ID", "__hour__", "ip", "SerialNumber.Config.RoomType"]}, {"type": "number", "label": "measure_value"}]}

将其加载到数据框中最快的方法是什么?

最佳答案

我在这里可能是错的,但这是否接近您要找的东西?因为我肯定能找到一种不那么脏的方法,但这里的输出很重要。

import pandas as pd

data = {"cache_age_milliseconds": 0, "rows": [{"values": [["Sonos_HXXpu71TY1g4HWWU2jXCJ8tcKu", 1483225200000, "87.61.241.100", "*null*"], 0.3605555555555556]}, {"values": [["Sonos_HXXpu71TY1g4HWWU2jXCJ8tcKu", 1483221600000, "87.61.241.100", "*null*"], 0.35888888888888887]}], "columns": [{"type": "array", "label": ["Household ID", "__hour__", "ip", "SerialNumber.Config.RoomType"]}, {"type": "number", "label": "measure_value"}]}
df = pd.DataFrame.from_dict([i["values"][0] for i in data["rows"]])
df.columns = data["columns"][0]["label"]
df.index = [i["values"][1] for i in data["rows"]]
df.index.name = data["columns"][1]["label"]

结果:

                                   Household ID       __hour__             ip SerialNumber.Config.RoomType
measure_value
0.360556 Sonos_HXXpu71TY1g4HWWU2jXCJ8tcKu 1483225200000 87.61.241.100 *null*
0.358889 Sonos_HXXpu71TY1g4HWWU2jXCJ8tcKu 1483221600000 87.61.241.100 *null*

关于python - 将 json dict 加载到数据框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45361788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com