In [1]:
import pandas as pd
import numpy as np
import pickle
In [3]:
## load all cat from pickle
#df = pd.read_pickle('../data_all_cities/all_cities_preprocess.pkl')
# df.head(2)
In [2]:
import pandas as pd
import numpy as np
def create_cuisine(path_df, tar_column, cuisine, path_culture, path_cuisine, save = False):
"""
This function creates a columne with spefic cruisine types
Attribute:
path_df: imported dataframe with businesses and a column of lists of categories
tar_column: the columne in the df used to compare with our lists
cuisine: name of the created cuisine type column
path_culture: file path of the text file with a list of cultural words
path_cuisine: file path of the text file with a list of selected cuisine words
save: change it to "True" if save the output to pickle file
Returns the original dataframe with a new cuisine column
-- 2 if the business category belongs to the selected cuisine
-- 1 if Not the selected cuisine (but with other region/culture related words)
-- 0 if it has no cultural labels
"""
# load dataframe
df = pd.read_pickle(path_df)
# load saved txt file
list_culture = open(path_culture, 'rw').read().split('\n')
list_cuisine = open(path_cuisine, 'rw').read().split('\n')
# assign numbers to each business
type_cuisine = df[tar_column].apply(lambda l: 2 if len(set(l).intersection(list_cuisine)) > 0 \
else 0 if len(set(l).intersection(list_culture)) == 0 \
else 1)
df['cuisine_{}'.format(cuisine)] = type_cuisine
if save:
df.to_pickle('Yelp_Cuisine_{}.p'.format(cuisine))
return df
In [ ]:
In [3]:
# parameters
path_df = '../data_all_cities/all_cities_preprocess.pkl'
path_culture = 'cat_culture.txt'
path_cuisine = 'cat_culture-Chinese.txt'
cuisine = 'Chinese'
tar_column = 'categories'
In [4]:
# create column for Chinese cuisine type
df_chinese = create_cuisine(path_df, tar_column, cuisine, path_culture, path_cuisine, save = False)
In [5]:
df_chinese.head(3)
Out[5]:
In [54]:
# Check
# df_chinese[df_chinese.cuisine_Chinese == 0]
In [6]:
df_chinese.shape
Out[6]:
In [7]:
df_chinese.groupby(df_chinese.cuisine_Chinese).size()
Out[7]:
In [8]:
df_chinese.to_pickle('Yelp_Cuisine_Chinese.pkl')
In [4]:
# parameters
path_df = '../data_all_cities/all_cities_preprocess.pkl'
path_culture = 'cat_culture.txt'
path_cuisine = 'cat_culture-Japanese.txt'
cuisine = 'Japanese'
tar_column = 'categories'
In [5]:
# create column for Chinese cuisine type
df_japanese = create_cuisine(path_df, tar_column, cuisine, path_culture, path_cuisine, save = False)
In [26]:
#df_japanese.head(2)
In [8]:
df_japanese.shape
Out[8]:
In [9]:
df_japanese.groupby(df_japanese.cuisine_Japanese).size()
Out[9]:
In [11]:
# save
df_japanese.to_pickle('Yelp_Cuisine_Japanese.pkl')
In [18]:
# parameters
path_df = '../data_all_cities/all_cities_preprocess.pkl'
path_culture = 'cat_culture.txt'
path_cuisine = 'cat_culture-American.txt'
cuisine = 'American'
tar_column = 'categories'
# create column for Indian cuisine type
df_american = create_cuisine(path_df, tar_column, cuisine, path_culture, path_cuisine, save = False)
df_american.shape
Out[18]:
In [19]:
df_american.groupby(df_american.cuisine_American).size()
Out[19]:
In [20]:
# save
df_american.to_pickle('Yelp_Cuisine_American.pkl')
In [15]:
# parameters
path_df = '../data_all_cities/all_cities_preprocess.pkl'
path_culture = 'cat_culture.txt'
path_cuisine = 'cat_culture-Indian.txt'
cuisine = 'Indian'
tar_column = 'categories'
# create column for Indian cuisine type
df_indian = create_cuisine(path_df, tar_column, cuisine, path_culture, path_cuisine, save = False)
df_indian.shape
Out[15]:
In [22]:
df_indian.groupby(df_indian.cuisine_Indian).size()
Out[22]:
In [21]:
# save
df_indian.to_pickle('Yelp_Cuisine_Indian.pkl')
In [23]:
# parameters
path_df = '../data_all_cities/all_cities_preprocess.pkl'
path_culture = 'cat_culture.txt'
path_cuisine = 'cat_culture-Spanish.txt'
cuisine = 'Spanish'
tar_column = 'categories'
# create column for Indian cuisine type
df_spanish = create_cuisine(path_df, tar_column, cuisine, path_culture, path_cuisine, save = False)
df_spanish.shape
Out[23]:
In [24]:
df_spanish.groupby(df_spanish.cuisine_Spanish).size()
Out[24]:
In [25]:
# save
df_spanish.to_pickle('Yelp_Cuisine_Spanish.pkl')
In [ ]:
In [ ]: