text_html_output_1.png
from json  import loads , dumps

import numpy as np

import pandas as pd

file = open('/kaggle/input/swiggy-restaurants-dataset/data.json', 'r')
data = file.read()
file.close()
js = loads(data)

cuisines = []
for i in js['Abohar']['restaurants'].keys():
    cuisines += js['Abohar']['restaurants'][i]['cuisine'].split(',')
cuisines = list(set(cuisines))

pop_cui = []
for cuisine in cuisines:
    c = 0
    for i in js['Abohar']['restaurants'].keys():
        if cuisine in js['Abohar']['restaurants'][i]['cuisine']:
            c += 1
    pop_cui.append([cuisine, c])
df = pd.DataFrame(pop_cui, columns=['item', 'freq'])
df[df['freq'] == max(df['freq'])]

pop_cui = []
for cuisine in cuisines:
    c = 0
    for i in js['Abohar']['restaurants'].keys():
        if cuisine in js['Abohar']['restaurants'][i]['cuisine']:
            c += 1
    pop_cui.append([cuisine, c])
df = pd.DataFrame(pop_cui, columns=['item', 'freq'])
df.sort_values(by='freq', ascending=False).head(5)

cost = []
for i in js['Abohar']['restaurants'].keys():
    cost.append(int(js['Abohar']['restaurants'][i]['cost'].split(' ')[-1]))
avg_cost = round(sum(cost) / len(cost), 2)

price = []
for i in js['Abohar']['restaurants'].keys():
    if 'North Indian' in js['Abohar']['restaurants'][i]['cuisine']:
        price.append(int(js['Abohar']['restaurants'][i]['cost'].split(' ')[-1]))

avg_cuisine = []
for cuisine in cuisines:
    price = []
    for i in js['Abohar']['restaurants'].keys():
        if cuisine in js['Abohar']['restaurants'][i]['cuisine']:
            price.append(int(js['Abohar']['restaurants'][i]['cost'].split(' ')[-1]))
    avg_cuisine.append([cuisine, sum(price) // len(price)])
avg_cuisine = pd.DataFrame(avg_cuisine, columns=['cuisine', 'avg_cost'])

rest_city = []
for i in js.keys():
    c = 0
    if 'restaurants' in js[i].keys():
        c = len(js[i]['restaurants'])
    else:
        for region in js[i].keys():
            if 'restaurants' in js[i][region].keys():
                c = len(js[i][region]['restaurants'])
    rest_city.append([i, c])
rest_city = pd.DataFrame(rest_city, columns=['city', 'total_restaurants'])

c = 0
arr = []
for city in js.keys():
    if 'restaurants' in js[city].keys():
        for rest in js[city]['restaurants'].keys():
            if 'menu' in js[city]['restaurants'][rest].keys():
                if len(js[city]['restaurants'][rest]['menu'].keys()) == 0:
                    c += 1
                    arr.append([rest])
                else:
                    c += 1
                    arr.append([rest])
    else:
        for regions in js[city].keys():
            if 'restaurants' in js[city][regions].keys():
                for rest in js[city][regions]['restaurants'].keys():
                    if 'menu' in js[city][regions]['restaurants'][rest].keys():
                        if len(js[city][regions]['restaurants'][rest]['menu'].keys()) == 0:
                            c += 1
                            arr.append([rest])
                    else:
                        c += 1
                        arr.append([rest])
arr = np.array(arr)
np.save('incompleted_rest_data.npy', arr)

avg_cost = []
for city in js.keys():
    cost = []
    if 'restaurants' in js[city].keys():
        for rest in js[city]['restaurants'].keys():
            try:
                cost.append(int(js[city]['restaurants'][rest]['cost'].split(' ')[-1]))
            except:
                pass
    else:
        for region in js[city].keys():
            if 'restaurants' in js[city][region].keys():
                for rest in js[city][region]['restaurants'].keys():
                    try:
                        cost.append(int(js[city][region]['restaurants'][rest]['cost'].split(' ')[-1]))
                    except:
                        pass
    try:
        avg_cost.append([city, sum(cost) // len(cost)])
    except:
        pass
df_ = pd.DataFrame(avg_cost, columns=['city', 'avg_cost'])
df_.sort_values(by='avg_cost', ascending=False).head(5)