我有一个地图,其中包含许多带有随机名称的json文件。每个文件都有一个嵌套对象。我想将文件的数据放入panda数据框中,第一层是嵌套对象的标识符。
文件如下。我有以下标识符:vendor_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:无法设置列不匹配的行
答案 0 :(得分:1)
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')