def agg_conc(original_countries,
             aggregates,
             missing_countries='test',
             merge_multiple_string='_&_',
             log_missing_countries=None,
             log_merge_multiple_strings=None,
             coco=None,
             as_dataframe='sparse',
             original_countries_class=None):
    """ Builds an aggregation concordance dict, vec or matrix

    Parameters
    ----------

    original_countries: list or str
        List of countries to aggregated, also accepts and valid column name of
        CountryConverter.data

    aggregates: list of dict or str
        List of aggregation information. This can either be dict mapping the
        names of 'original_countries' to aggregates, or a valid column name of
        CountryConverter.data Aggregation happens in order given in this
        parameter.  Thus, country assigned to an aggregate are not re-assigned
        by the following aggregation information.

    missing_countries: str, boolean, None
        Entry to fill in for countries in 'original_countries' which do not
        appear in 'aggregates'.  str: Use the given name for all missing
        countries True: Use the name in original_countries for missing
        countries False: Skip these countries None: Use None for these
        countries

    merge_multiple_string: str or None, optional
        If multiple correspondance entries are given in one of the aggregates
        join them with the given string (default: '_&_'.  To skip these enries,
        pass None.

    log_missing_countries: function, optional
        This function is called with country is country is in
        'original_countries' but missing in all 'aggregates'.
        For example, pass
        lambda x: logging.error('Country {} missing'.format(x))
        to log errors for such countries.  Default: do nothing

    log_merge_multiple_strings: function, optional
        Function to call for logging multiple strings, see
        log_missing_countries Default: do nothing

    coco: instance of CountryConverter, optional
        CountryConverter instance used for the conversion.  Pass a custom one
        if additional data is needed in addition to the custom country
        converter file.  If None (default), the bare CountryConverter is used

    as_dataframe: boolean or st, optional
        If False, output as OrderedDict.  If True or str, output as pandas
        dataframe.  If str and 'full', output as a full matrix, otherwise only
        two collumns with the original and aggregated names are returned.

    original_countries_class: str, optional
        Valid column name of CountryConverter.data.  This parameter is needed
        if a list of countries is passed to 'orginal_countries' and strings
        corresponding to data in CountryConverter.data are used subsequently.
        Can be omitted otherwise.

    Returns
    -------

    OrderedDict or DataFrame (defined by 'as_dataframe')

    """

    if coco is None:
        coco = CountryConverter()

    if type(original_countries) is str:
        original_countries_class = original_countries
        original_countries = coco.data[original_countries].values
    else:
        original_countries_class = (original_countries_class or
                                    coco._get_input_format_from_name(
                                        original_countries[0]))

    if type(aggregates) is not list:
        aggregates = [aggregates]

    correspond = OrderedDict.fromkeys(original_countries)

    for agg in aggregates:
        if type(agg) is str:
            agg = coco.get_correspondance_dict(original_countries_class,
                                               agg)
        for country in original_countries:
            if correspond.get(country) is None:
                try:
                    entry = agg[country]
                except KeyError:
                    entry = None

                if type(entry) is list:
                    if 1 < len(entry):
                        if merge_multiple_string:
                            entry = merge_multiple_string.join([
                                str(e) for e in entry])
                        else:
                            entry = None
                        if log_merge_multiple_strings:
                            log_merge_multiple_strings(country)
                    else:
                        entry = entry[0]

                correspond[country] = entry

    for country in original_countries:
        if correspond.get(country) is None:
            if missing_countries is True:
                correspond[country] = country
            elif missing_countries is False:
                del correspond[country]
            else:
                correspond[country] = missing_countries
            if log_missing_countries:
                log_missing_countries(country)

    if as_dataframe:
        correspond = pd.DataFrame.from_dict(
            correspond, orient='index').reset_index()
        correspond.columns = ['original', 'aggregated']
        if ((type(as_dataframe) is str) and
                (as_dataframe[0].lower() == 'f')):
            _co_list = correspond.original
            correspond['val'] = 1
            correspond = correspond.set_index(
                ['original', 'aggregated']).unstack().fillna(0)['val']
            correspond = correspond.loc[_co_list]

    return correspond
def agg_conc ( original_countries , aggregates , missing_countries = 'test' , merge_multiple_string = '_&_' , log_missing_countries = None , log_merge_multiple_strings = None , coco = None , as_dataframe = 'sparse' , original_countries_class = None ) : if coco is None : coco = CountryConverter ( ) if type ( original_countries ) is str : original_countries_class = original_countries original_countries = coco . data [ original_countries ] . values else : original_countries_class = ( original_countries_class or coco . _get_input_format_from_name ( original_countries [ 0 ] ) ) if type ( aggregates ) is not list : aggregates = [ aggregates ] correspond = OrderedDict . fromkeys ( original_countries ) for agg in aggregates : if type ( agg ) is str : agg = coco . get_correspondance_dict ( original_countries_class , agg ) for country in original_countries : if correspond . get ( country ) is None : try : entry = agg [ country ] except KeyError : entry = None if type ( entry ) is list : if 1 < len ( entry ) : if merge_multiple_string : entry = merge_multiple_string . join ( [ str ( e ) for e in entry ] ) else : entry = None if log_merge_multiple_strings : log_merge_multiple_strings ( country ) else : entry = entry [ 0 ] correspond [ country ] = entry for country in original_countries : if correspond . get ( country ) is None : if missing_countries is True : correspond [ country ] = country elif missing_countries is False : del correspond [ country ] else : correspond [ country ] = missing_countries if log_missing_countries : log_missing_countries ( country ) if as_dataframe : correspond = pd . DataFrame . from_dict ( correspond , orient = 'index' ) . reset_index ( ) correspond . columns = [ 'original' , 'aggregated' ] if ( ( type ( as_dataframe ) is str ) and ( as_dataframe [ 0 ] . lower ( ) == 'f' ) ) : _co_list = correspond . original correspond [ 'val' ] = 1 correspond = correspond . set_index ( [ 'original' , 'aggregated' ] ) . unstack ( ) . fillna ( 0 ) [ 'val' ] correspond = correspond . loc [ _co_list ] return correspond
Builds an aggregation concordance dict, vec or matrix

    Parameters
    ----------

    original_countries: list or str
        List of countries to aggregated, also accepts and valid column name of
        CountryConverter.data

    aggregates: list of dict or str
        List of aggregation information. This can either be dict mapping the
        names of 'original_countries' to aggregates, or a valid column name of
        CountryConverter.data Aggregation happens in order given in this
        parameter.  Thus, country assigned to an aggregate are not re-assigned
        by the following aggregation information.

    missing_countries: str, boolean, None
        Entry to fill in for countries in 'original_countries' which do not
        appear in 'aggregates'.  str: Use the given name for all missing
        countries True: Use the name in original_countries for missing
        countries False: Skip these countries None: Use None for these
        countries

    merge_multiple_string: str or None, optional
        If multiple correspondance entries are given in one of the aggregates
        join them with the given string (default: '_&_'.  To skip these enries,
        pass None.

    log_missing_countries: function, optional
        This function is called with country is country is in
        'original_countries' but missing in all 'aggregates'.
        For example, pass
        lambda x: logging.error('Country {} missing'.format(x))
        to log errors for such countries.  Default: do nothing

    log_merge_multiple_strings: function, optional
        Function to call for logging multiple strings, see
        log_missing_countries Default: do nothing

    coco: instance of CountryConverter, optional
        CountryConverter instance used for the conversion.  Pass a custom one
        if additional data is needed in addition to the custom country
        converter file.  If None (default), the bare CountryConverter is used

    as_dataframe: boolean or st, optional
        If False, output as OrderedDict.  If True or str, output as pandas
        dataframe.  If str and 'full', output as a full matrix, otherwise only
        two collumns with the original and aggregated names are returned.

    original_countries_class: str, optional
        Valid column name of CountryConverter.data.  This parameter is needed
        if a list of countries is passed to 'orginal_countries' and strings
        corresponding to data in CountryConverter.data are used subsequently.
        Can be omitted otherwise.

    Returns
    -------

    OrderedDict or DataFrame (defined by 'as_dataframe')