src/03_modelling/07_feature_engineering.py
def chain(df):
        """This function counts how many times a business name
            occurs in the entire dataframe.

           It is not aggregated to years in order to capture
            the scenario of a business gone out of business
            for a couple of years but came back at a different
            location later on.

           When counting chain businesses, both business name
            and business industry are used. This is because
            some business names are owner's names so there are
            duplicated names for completely different businesses.
        """

        # count business by name so filter out the ones
        #   without a name first
        df_copy = df[df.BusinessName.notnull()]

        names = []

        # use business_id because ids are aggregated
        #  using location. e.g., Starbucks at different locations
        #  will have the same name but different ids
        for i in df_copy.business_id.unique():
            names.append(
                (df_copy.loc[df_copy.business_id == i,
                             'BusinessName'].values[0],
                 df_copy.loc[df_copy.business_id == i,
                             'BusinessIndustry'].values[0]))

        # count names
        name_dict = Counter(names)

        # add chain column
        chain = []
        for i in range(len(df)):
            name = df.iloc[i, df.columns.get_loc(
                'BusinessName')]
            industry = df.iloc[i, df.columns.get_loc(
                'BusinessIndustry')]

            if pd.isnull(name):
                chain.append(name)
            else:
                try:
                    chain.append(name_dict[
                        (name, industry)])
                except:
                    chain.append(0)

        df['chain'] = chain

        return df
This function counts how many times a business name
            occurs in the entire dataframe.

           It is not aggregated to years in order to capture
            the scenario of a business gone out of business
            for a couple of years but came back at a different
            location later on.

           When counting chain businesses, both business name
            and business industry are used. This is because
            some business names are owner's names so there are
            duplicated names for completely different businesses.
        
This function counts how many times a business name
occurs in the entire dataframe.

It is not aggregated to years in order to capture
the scenario of a business gone out of business
for a couple of years but came back at a different
location later on.

When counting chain businesses, both business name
and business industry are used. This is because
some business names are owner's names so there are
duplicated names for completely different businesses.
This function counts how many times a business name occurs in the entire dataframe . It is not aggregated to years in order to capture the scenario of a business gone out of business for a couple of years but came back at a different location later on . When counting chain businesses both business name and business industry are used . This is because some business names are owner ' s names so there are duplicated names for completely different businesses .
def chain(df):
        df_copy = df[df.BusinessName.notnull()]
        names = []
        for i in df_copy.business_id.unique():
            names.append(
                (df_copy.loc[df_copy.business_id == i,
                             'BusinessName'].values[0],
                 df_copy.loc[df_copy.business_id == i,
                             'BusinessIndustry'].values[0]))
        name_dict = Counter(names)
        chain = []
        for i in range(len(df)):
            name = df.iloc[i, df.columns.get_loc(
                'BusinessName')]
            industry = df.iloc[i, df.columns.get_loc(
                'BusinessIndustry')]
            if pd.isnull(name):
                chain.append(name)
            else:
                try:
                    chain.append(name_dict[
                        (name, industry)])
                except:
                    chain.append(0)
        df['chain'] = chain
        return df
def chain ( df ) : df_copy = df [ df . BusinessName . notnull ( ) ] names = [ ] for i in df_copy . business_id . unique ( ) : names . append ( ( df_copy . loc [ df_copy . business_id == i , 'BusinessName' ] . values [ 0 ] , df_copy . loc [ df_copy . business_id == i , 'BusinessIndustry' ] . values [ 0 ] ) ) name_dict = Counter ( names ) chain = [ ] for i in range ( len ( df ) ) : name = df . iloc [ i , df . columns . get_loc ( 'BusinessName' ) ] industry = df . iloc [ i , df . columns . get_loc ( 'BusinessIndustry' ) ] if pd . isnull ( name ) : chain . append ( name ) else : try : chain . append ( name_dict [ ( name , industry ) ] ) except : chain . append ( 0 ) df [ 'chain' ] = chain return df
This function counts how many times a business name
occurs in the entire dataframe.
This function counts how many times a business name occurs in the entire dataframe .
"""This function counts how many times a business name
            occurs in the entire dataframe.

           It is not aggregated to years in order to capture
            the scenario of a business gone out of business
            for a couple of years but came back at a different
            location later on.

           When counting chain businesses, both business name
            and business industry are used. This is because
            some business names are owner's names so there are
            duplicated names for completely different businesses.
        """ # count business by name so filter out the ones #   without a name first # use business_id because ids are aggregated #  using location. e.g., Starbucks at different locations #  will have the same name but different ids # count names # add chain column
df