Skip to content

Instantly share code, notes, and snippets.

@JohnnyFang
Created April 28, 2019 00:36
Show Gist options
  • Save JohnnyFang/8a2e81293649e49247e5aa5697b67ed2 to your computer and use it in GitHub Desktop.
Save JohnnyFang/8a2e81293649e49247e5aa5697b67ed2 to your computer and use it in GitHub Desktop.
loading zomato json restaurant data
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