首页 > 解决方案 > 将嵌套的 json 文件列表转换为 pandas 数据框

问题描述

我有一张地图,里面有很多随机名称的 json 文件。每个文件都有一个嵌套对象。我想将文件的数据放入熊猫数据框中,第一级是嵌套对象的标识符。

该文件如下。我有以下标识符:seller_name、seller_location、sample_time、seller_average_response_time、fiverr_url、“seller_registration_time、gig_title。评论是嵌套对象。

我想要一个数据框,它为每一行放置标识符,每行一个评论。我听说我必须为此使用某个melt命令。

你能给出一个示例代码吗?

{"seller_name": "let_me_do_it_", 
"seller_location": "Austria", 
"sample_time": "21-11-2018", 
"reviews": 
[{"review_time": "about 1 year ago", 
"buyer_comment": "Good communication.", 
"buyer_name": "fivejobus", 
"buyer_feedback_rating": "5"}, 
{"review_time": "about 1 year ago", 
"buyer_comment": "Good! Thanks.", "buyer_name": "ericzhu1204",
"buyer_feedback_rating": "5"}, {"review_time": "about 1 year ago", 
"buyer_comment": "Delivery on time and Good communication,", 
"buyer_name": "fivejobus", "buyer_feedback_rating": "5"}], 
"seller_average_response_time": "", 
"fiverr_url": "https://www.fiverr.com/let_me_do_it_/translate-your-text-in-well-written-english-or-german?context&context_referrer=search_gigs&context_type=auto&pos=39&ref_ctx_id=b833b214-2869-487b-9721-fb91c0a18fb6&funnel=a316bb03-214f-44ee-a234-58e1bc3ed8e1", 
"seller_registration_time": "Aug 2017", 
"gig_title": "I will translate your english text to well written german"}

目前,我有这个:

import os, json
import pandas as pd

path_to_json = '/Users/rogier/Downloads/data'
json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')]
#print(json_files)  # for me this prints ['foo.json']

jsons_data = pd.DataFrame(columns=(['sellername', 'sellerlocation', 'sampletime', 'selleraverageresponsetime', 'fiverr_url', 'gigtitle'], ['review_time','buyer_comment','buyer_name','buyer_feedback_rating']))

for index, js in enumerate(json_files):
    with open(os.path.join(path_to_json, js)) as json_file:
        json_text = json.load(json_file)

        sellername = json_text['seller_name']
        sellerlocation=json_text['seller_location']
        sampletime=json_text['sample_time']

        jsons_data.loc[index] = [sellername, sellerlocation, sampletime]

我收到此错误:

ValueError:无法设置列不匹配的行

标签: pythonjsonpandas

解决方案


apply+Series

df = pd.DataFrame(my_dict)
review_data = df.reviews.apply(pd.Series)
new_df = pd.concat([df,review_data], axis = 1).drop(['reviews'], axis = 1)

这会将字典的每个字段添加为原始的新列df

print(df.columns)
Index(['fiverr_url', 'gig_title', 'sample_time',
   'seller_average_response_time', 'seller_location', 'seller_name',
   'seller_registration_time', 'buyer_comment', 'buyer_feedback_rating',
   'buyer_name', 'review_time'],
  dtype='object')

推荐阅读