我已将嵌套的JSON文件转换为pandas DataFrame。有些列现在包含列表。它们看起来像这样:
0 [BikeParking: True, BusinessAcceptsBitcoin: Fa...
1 [BusinessAcceptsBitcoin: False, BusinessAccept...
2 [Alcohol: none, Ambience: {'romantic': False, ...
3 [AcceptsInsurance: False, BusinessAcceptsCredi...
4 [BusinessAcceptsCreditCards: True, Restaurants...
5 [BusinessAcceptsCreditCards: True, ByAppointme...
6 [BikeParking: True, BusinessAcceptsCreditCards...
7 [Alcohol: none, Ambience: {'romantic': False, ...
8 [BusinessAcceptsCreditCards: True]
9 [BikeParking: True, BusinessAcceptsCreditCards...
10 None
.
.
.
144070 [Alcohol: none, Ambience: {'romantic': False, ...
144071 [BikeParking: True, BusinessAcceptsCreditCards...
Name: attributes, dtype: object
和此:
0 [Monday 11:0-21:0, Tuesday 11:0-21:0, Wednesda...
1 [Monday 0:0-0:0, Tuesday 0:0-0:0, Wednesday 0:...
2 [Monday 11:0-2:0, Tuesday 11:0-2:0, Wednesday ...
3 [Tuesday 10:0-21:0, Wednesday 10:0-21:0, Thurs...
4 None
144066 None
144067 [Tuesday 8:0-16:0, Wednesday 8:0-16:0, Thursda...
144068 [Tuesday 10:0-17:30, Wednesday 10:0-17:30, Thu...
144069 None
144070 [Monday 11:0-20:0, Tuesday 11:0-20:0, Wednesda...
144071 [Monday 10:0-21:0, Tuesday 10:0-21:0, Wednesda...
Name: hours, dtype: object
我是否有办法自动提取标签(BikeParking,AcceptsInsurance等)并将其用作列名,同时使用true / false值填充单元格。对于Ambience dict,我想在单元格中执行类似Ambience_romantic和true / false的操作。同样,我想以列名的形式提取一周中的几天,并使用小时来填充单元格。
或者有没有办法在之前展平json数据?我已经尝试将json数据逐行传递给json_normalize并从输出创建数据帧,但它产生相同的结果。也许我做错了什么?
原始json的格式(yelp_academic_dataset_business.json):
{
"business_id":"encrypted business id",
"name":"business name",
"neighborhood":"hood name",
"address":"full address",
"city":"city",
"state":"state -- if applicable --",
"postal code":"postal code",
"latitude":latitude,
"longitude":longitude,
"stars":star rating, rounded to half-stars,
"review_count":number of reviews,
"is_open":0/1 (closed/open),
"attributes":["an array of strings: each array element is an attribute"],
"categories":["an array of strings of business categories"],
"hours":["an array of strings of business hours"],
"type": "business"
}
我使用json_normalize进行初始尝试:
with open('yelp_academic_dataset_business.json') as f:
#Normalize the json data to flatten it and store output in a dataframe
frame= json_normalize([json.loads(line) for line in f])
#write the dataframe to a csv file
frame.to_csv('yelp_academic_dataset_business.csv', encoding='utf-8', index=False)
我目前正在尝试的内容:
with open(json_filename) as f:
data = f.readlines()
# remove the trailing "\n" from each line
data = map(lambda x: x.rstrip(), data)
data_json_str = "[" + ','.join(data) + "]"
df = read_json(data_json_str)
#Now Looking to expand df['attributes'] and others here
我还应该提到我的目标是将其转换为.csv以将其加载到数据库中。我不想在我的数据库列中找到列表。
您可以从Yelp数据集挑战网站获取原始json数据: https://www.yelp.ca/dataset_challenge/dataset
答案 0 :(得分:0)
您正在尝试将“文档”(半结构化数据)转换为表格。如果一个记录包含例如,这可能是有问题的。没有其他记录的100个属性 - 您可能不希望向主表添加100列,并且所有其他记录都有空单元格。
但最后你已经解释过你打算这样做:
我在这里告诉你,这一切都毫无意义。通过所有这些中间格式对数据进行捣碎只会导致问题。
相反,让我们回到基础:
现在第一步是提出架构。或者,如果您使用的是NoSQL数据库,则可以直接加载JSON而无需其他步骤。