<jupyter_start><jupyter_text>https://www.kaggle.com/himanshupoddar/zomato-bangalore-restaurants<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.style as style
style.use('dark_background')

path = '/content/drive/MyDrive/Colab Notebooks/EDA_project/'
df = pd.read_csv(path + "restaurants.csv")
df.head()
df.shape
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 51717 entries, 0 to 51716
Data columns (total 17 columns):
 #   Column                       Non-Null Count  Dtype 
---  ------                       --------------  ----- 
 0   url                          51717 non-null  object
 1   address                      51717 non-null  object
 2   name                         51717 non-null  object
 3   online_order                 51717 non-null  object
 4   book_table                   51717 non-null  object
 5   rate                         43942 non-null  object
 6   votes                        51717 non-null  int64 
 7   phone                        50509 non-null  object
 8   location                     51696 non-null  object
 9   rest_type                    51490 non-null  object
 10  dish_liked                   23639 non-null  object
 11  cuisines                     51672 non-null  object
 12  approx_cost(for two people)  51371 non-null  object
 13  reviews_list                 51[...]<jupyter_text>**1. Data cleaning** **1.1. Removing unnecessary columns**<jupyter_code>df.drop(columns=['url','phone','dish_liked'], inplace=True)<jupyter_output><empty_output><jupyter_text>**1.2 Handling NULL values**<jupyter_code>df.isna().sum()
df.dropna(inplace = True)
df.isna().sum()<jupyter_output><empty_output><jupyter_text>**1.3 Renamimg columns**<jupyter_code>df.rename(columns={"listed_in(type)": "type_of_meal", "listed_in(city)": "city" , "approx_cost(for two people)": "cost"},inplace=True)
df.columns<jupyter_output><empty_output><jupyter_text>**1.4 Data Type transformation**<jupyter_code>#Converting cost type to float
print(df.cost.dtypes) 
df.cost = df.cost.astype('string') 

df.cost = df.cost.str.replace(',','')

df.cost = df.cost.astype('float')
print(df.cost.dtypes)
df.cost.value_counts()
df.rate.unique()
#Getting rid of '-' and 'NEW' and Converting rate type to float 
df = df[(df.rate != 'NEW') & (df.rate != '-')]
df.rate = df.rate.str[0:3]
df.rate = df.rate.astype('float')
df.rate.head()
#Convert all 'Yes' and 'No' in Data Frame to True and False
df.replace({'Yes':True,'No':False},inplace = True)<jupyter_output><empty_output><jupyter_text>**1.3 Handling duplicates**<jupyter_code># Cheking for duplicates
print(df.duplicated().sum())
df.drop_duplicates(inplace = True)
print(df.duplicated().sum())<jupyter_output>26
0<jupyter_text>**Checking for restaurants with few branches**<jupyter_code>df.shape
df_grp_address_name = df.drop_duplicates(subset=['address','name'])
#Number of restaurants in data(branches)
df_grp_address_name.shape[0]
df.duplicated(subset=['address','name','type_of_meal']).sum()
df_grp_address_name_type_of_meal = df.drop_duplicates(subset=['address','name','type_of_meal'])
df_grp_address_name_type_of_meal.shape
df_grp_name = df.drop_duplicates(subset=['name'])
print('Number of Restaurant\'s branches in data:',df_grp_address_name.shape[0])
print('Number of Restaurant chain in data:',df_grp_name.shape[0])<jupyter_output>Number of Restaurant's branches in data: 9438
Number of Restaurant chain in data: 6602<jupyter_text>**2. Data investigation**<jupyter_code># df_grp_address_name.corr()<jupyter_output><empty_output><jupyter_text>How much restaurants have more than one branch?<jupyter_code>df_with_few_branches = df_grp_address_name[df_grp_address_name\
                                                                .duplicated(['name'],keep=False)].sort_values('name',ascending=False)

print(df_with_few_branches.name.nunique())
df_with_few_branches<jupyter_output>970<jupyter_text>We can see than it possible that in one branch it allow to book a table and in another one it's not althougt they are belong to the same chain (like "eat.fit")Also it possible that in one branch it allow to have an online order and in another one it's not althougt they are belong to the same chain (like "1522-The pub") Which is the Restaurant chain with most branches?<jupyter_code>df_grp_address_name.name.value_counts().head(1)<jupyter_output><empty_output><jupyter_text>How much branches allowing online orders?<jupyter_code>df.drop_duplicates(subset=['address','name','online_order']).online_order.value_counts().plot(kind="bar", color=['green','blue'],\
                                                figsize = (5,6), fontsize=10, title='Restaurant allowing online orders').axes.title.set_size(20)<jupyter_output><empty_output><jupyter_text>How much restaurants allowing book table?<jupyter_code>df[df.type_of_meal != 'Delivery'].drop_duplicates(subset=['address','name','book_table']).book_table.value_counts()\
                                                  .plot(kind="bar", color=['blue','green'],\
                                                  figsize = (5,6), fontsize=10, title='Restaurant allowing book table').axes.title.set_size(20)<jupyter_output><empty_output><jupyter_text>What is the precentage of restaurant allowing book table for each rate?<jupyter_code>df_no_repeat = df.drop_duplicates(subset=['address','name','book_table'])

df_rate_book_table = pd.crosstab(df_no_repeat.rate, df_no_repeat.book_table)
df_rate_book_table.div(df_rate_book_table.sum(1).astype('float'),axis=0)\
                  .plot(kind='bar',stacked=True,color=['blue','green'],figsize = (10,10), fontsize=20, title='Precentage of restaurant allowing book table\nfor each rate ')\
                  .axes.title.set_size(20)
plt.legend(loc='upper right')<jupyter_output><empty_output><jupyter_text>Which places have the most restaurants?<jupyter_code>df_no_repeat = df.drop_duplicates(subset=['address','name','city'])
df_no_repeat.city.value_counts(ascending=True).plot(kind='bar',figsize = (10,10),colormap='Paired',edgecolor='black',fontsize=10)<jupyter_output><empty_output><jupyter_text>Which ten places has the most high-rated restaurants? (High rate = 4 or more)<jupyter_code>df_no_repeat = df.drop_duplicates(subset=['address','name','city','rate'])
df_no_repeat['is_high_rate'] = np.where(df_no_repeat['rate'] >= 4,True,False)
df_no_repeat_is_high_rate = df_no_repeat[df_no_repeat.is_high_rate == True]


df_rate_city_table = pd.crosstab(df_no_repeat_is_high_rate.city,df_no_repeat_is_high_rate.is_high_rate).sort_values(by=True,ascending=False)
df_rate_city_table.head(10).sort_values(by=True,ascending=True)\
                           .plot(kind='bar',color=['blue','green'],figsize = (10,10), fontsize=20, title='10 city with the big rates of their restaurants',legend=None)\
                           .axes.title.set_size(20)<jupyter_output>/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:2: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy<jupyter_text>*That good but stil not enough...We need to check the precntage Which ten places have the percentage of restaurants with the highest rate out of the total number of restaurants in those places<jupyter_code>df_rate_city_table = pd.crosstab(df_no_repeat.city,df_no_repeat.is_high_rate).sort_values(by=True,ascending=True)
df_rate_city_table
df_rate_city_table.div(df_rate_city_table.sum(1).astype('float'),axis=0)[True].sort_values(ascending=True).tail(10)\
                  .plot(kind='bar',color=['blue'],figsize = (10,10), fontsize=20, title='precentage of 10 big rates in each place')\
                  .axes.title.set_size(20)<jupyter_output><empty_output><jupyter_text>What types of restaurants exist?<jupyter_code>print('The types of restaurant:')
for i,type_of_food in enumerate(df.type_of_meal.unique(),1):
  print(f'{i}.',type_of_food)

# df.duplicated(subset=['name','type_of_meal']).sum()  
# df_name_and_type_of_meal_no_repeat = df.drop_duplicates(subset=['name','type_of_meal']) 
# df_name_and_type_of_meal_no_repeat.duplicated(subset=['name','type_of_meal']).sum()<jupyter_output>The types of restaurant:
1. Buffet
2. Cafes
3. Delivery
4. Desserts
5. Dine-out
6. Drinks & nightlife
7. Pubs and bars<jupyter_text>What is the amount type of food offered by the restaurant chains?<jupyter_code>df_no_repeat = df.drop_duplicates(subset=['name','type_of_meal'])
df_no_repeat['key'] = 1
pivot_table_name_and_type_of_meal_no_repeat = df_no_repeat.pivot_table(index='name', columns='type_of_meal'\
                                                                                            ,values='key', aggfunc='count',fill_value=0,margins = True)

pivot_table_name_and_type_of_meal_no_repeat.sort_values(by='All').All[:-1].value_counts()

#3682 restaurants chains have 2 types of food's branches<jupyter_output>/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:2: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy<jupyter_text>What is the amount of restaurant chain for each type of food?<jupyter_code>pivot_table_name_and_type_of_meal_no_repeat.drop('All',inplace = True,axis=1)
pivot_table_name_and_type_of_meal_no_repeat[-1:].transpose().plot(kind='bar',color=['blue'],figsize = (10,10), fontsize=20, \
                                                                        title='Amount of restaurant chain for each type of food',legend=None)\
                                                                        .axes.title.set_size(20)
#270 restaurant chains have at least one branch that offer Buffet<jupyter_output><empty_output><jupyter_text>What is the average cost for two in each retaurant type? (in branchs level)<jupyter_code>df_no_repeat = df.drop_duplicates(subset=['address','name','type_of_meal'])
pivot_table_cost_and_type_of_meal_no_repeat = df_no_repeat.pivot_table(index='type_of_meal'\
                                                                                            ,values='cost', aggfunc='mean',fill_value=0)

pivot_table_cost_and_type_of_meal_no_repeat.sort_values(by='cost').plot(kind='bar',color=['blue'],figsize = (10,10), fontsize=20, \
                                                                        title='Average cost for 2 in each restaurant type',legend=None)\
                                                                        .axes.title.set_size(20)<jupyter_output><empty_output><jupyter_text>What is the average cost for two in each place?<jupyter_code>df_no_repeat = df.drop_duplicates(subset=['address','name','city'])
pivot_table_cost_and_city_no_repeat = df_no_repeat.pivot_table(index='city',values='cost', aggfunc='mean',fill_value=0)

pivot_table_cost_and_city_no_repeat.sort_values(by='cost').plot(kind='bar',color=['blue'], fontsize=20,figsize = (10,10),\
                                                                        title='Average cost for 2 in each place',legend=None)\
                                                                        .axes.title.set_size(20)

# Indian rupee = 0.042 ISL<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12407936920544146, 'fraction_numerical': 0.021748548652629755, 'mean_word_length': 3.753706754530478, 'pattern_counts': {'":': 3, '<': 79, '<?xml version=': 0, '>': 80, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 35, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '21', 'dir': 'jupyter-structured-clean-dedup', 'id': '455199', 'license': 'MIT', 'n_tokens_mistral': 3968, 'n_tokens_neox': 3733, 'n_words': 689, 'path': 'EDA_project.ipynb', 'repo_name': 'yair11/DS_Bootcamp', 'size': '11541'}