Created
April 28, 2019 00:36
-
-
Save JohnnyFang/8a2e81293649e49247e5aa5697b67ed2 to your computer and use it in GitHub Desktop.
loading zomato json restaurant data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
path ="datasets/" | |
files = ["file1.json", "file2.json", "file3.json", "file4.json", "file5.json"] | |
column_names = ["restaurant.id", "restaurant.name", "restaurant.cuisines", "restaurant.location.city", | |
"restaurant.location.address", "restaurant.location.latitude", | |
"restaurant.location.longitude", "restaurant.user_rating.rating_text"] | |
new_col_names = ["id", "name", "cuisines", "city", "address", "latitude", "longitude", "rating_text"] | |
dataset = pd.DataFrame() | |
for file in files: | |
file_path = path + file | |
with open(file_path) as f: | |
json_data = json.load(f) | |
for el in json_data: | |
if 'restaurants' in el and len(el['restaurants']): | |
df = pd.concat([pd.DataFrame(json_normalize(restaurant)) for restaurant in el['restaurants']]) | |
restaurants_df = df[[col for col in column_names]] | |
restaurants_df.rename(columns=dict(zip(column_names, new_col_names)), inplace=True) | |
dataset = pd.concat([dataset, restaurants_df]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment