我如何在表格中转换JSON(使用Python)?

问题描述 投票:0回答:1

我有一个看起来像这样的JSON:

{ 
"data":{ 
"page_posts_count":{ 
"title":"Number of posts",
"value":9.0,
"formatted_value":"9"
},
"page_posts_per_day":{ 
"title":"Posts per day",
"value":0.32142857142857145,
"formatted_value":"0.3"
},
"page_posts_likes_count":{ 
"title":"Number of Likes",
"value":1558278.0,
"formatted_value":"1.6M"
},
"page_posts_comments_count":{ 
"title":"Number of Comments",
"value":173440.0,
"formatted_value":"173k"
},
"page_posts_shares_count":{ 
"title":"Number of Shares",
"value":78203.0,
"formatted_value":"78k"
},
"metadata":{ 
"version":"v1",
"profile_id":"6815841748",
"profile_name":"Barack Obama",
"token":"abcdefghijklmnopqrstuvwxyz",
"date_from":"Fri Dec 13 00:00:00 UTC 2019",
"date_until":"Thu Jan 09 23:59:59 UTC 2020",
"message":"No Insights returned.",
"network":"facebook",
"task":"kpi"
}
}

[当我尝试执行json_normalize时,它们给我以下错误:

 if any([isinstance(x, dict) for x in y.values()] for y in data):
          AttributeError: 'str' object has no attribute 'values'

而且我的数据框结尾看起来像这样:

                          Data                                                          Metadata
page_posts_count     { "title":"Number of posts", "value":9.0, "formatted_value":"9" }   nan

但是我需要的是这样的东西:

title              value       formatted_value
Number of posts    9.0            9

我什至不需要元数据部分。

所以有些想法将不胜感激。

谢谢!

python json pandas normalize
1个回答
0
投票

您可以直接使用熊猫的from_dict方法:

tmp = { 
    "data": { 
        "page_posts_count":{ 
        "title":"Number of posts",
        "value":9.0,
        "formatted_value":"9"
        },
        "page_posts_per_day":{ 
        "title":"Posts per day",
        "value":0.32142857142857145,
        "formatted_value":"0.3"
        },
        "page_posts_likes_count":{ 
        "title":"Number of Likes",
        "value":1558278.0,
        "formatted_value":"1.6M"
        },
        "page_posts_comments_count":{ 
        "title":"Number of Comments",
        "value":173440.0,
        "formatted_value":"173k"
        },
        "page_posts_shares_count":{ 
        "title":"Number of Shares",
        "value":78203.0,
        "formatted_value":"78k"
        },
        "metadata":{ 
        "version":"v1",
        "profile_id":"6815841748",
        "profile_name":"Barack Obama",
        "token":"abcdefghijklmnopqrstuvwxyz",
        "date_from":"Fri Dec 13 00:00:00 UTC 2019",
        "date_until":"Thu Jan 09 23:59:59 UTC 2020",
        "message":"No Insights returned.",
        "network":"facebook",
        "task":"kpi"
        }
    }
}
del tmp['data']['metadata'] # you don't need metadata

df = pd.DataFrame.from_dict(tmp['data'])

哪个给:

dataframe

© www.soinside.com 2019 - 2024. All rights reserved.