def average_percentage_of_new_providers(providers_info, providers_count):
    """
    Return the average percentage of new providers
    per segment and the average percentage of all projects.
    """
    segments_percentages = {}
    all_projects_percentages = []
    providers_count = providers_count.to_dict()[0]

    for _, items in providers_info.groupby('PRONAC'):
        cnpj_array = items['nrCNPJCPF'].unique()
        new_providers = 0
        for cnpj in cnpj_array:
            cnpj_count = providers_count.get(cnpj, 0)
            if cnpj_count <= 1:
                new_providers += 1

        segment_id = items.iloc[0]['idSegmento']
        new_providers_percent = new_providers / cnpj_array.size
        segments_percentages.setdefault(segment_id, [])
        segments_percentages[segment_id].append(new_providers_percent)
        all_projects_percentages.append(new_providers_percent)

    segments_average_percentage = {}
    for segment_id, percentages in segments_percentages.items():
        mean = np.mean(percentages)
        segments_average_percentage[segment_id] = mean

    return pd.DataFrame.from_dict({
        'segments_average_percentage': segments_average_percentage,
        'all_projects_average': np.mean(all_projects_percentages)
    })
def average_percentage_of_new_providers ( providers_info , providers_count ) : segments_percentages = { } all_projects_percentages = [ ] providers_count = providers_count . to_dict ( ) [ 0 ] for _ , items in providers_info . groupby ( 'PRONAC' ) : cnpj_array = items [ 'nrCNPJCPF' ] . unique ( ) new_providers = 0 for cnpj in cnpj_array : cnpj_count = providers_count . get ( cnpj , 0 ) if cnpj_count <= 1 : new_providers += 1 segment_id = items . iloc [ 0 ] [ 'idSegmento' ] new_providers_percent = new_providers / cnpj_array . size segments_percentages . setdefault ( segment_id , [ ] ) segments_percentages [ segment_id ] . append ( new_providers_percent ) all_projects_percentages . append ( new_providers_percent ) segments_average_percentage = { } for segment_id , percentages in segments_percentages . items ( ) : mean = np . mean ( percentages ) segments_average_percentage [ segment_id ] = mean return pd . DataFrame . from_dict ( { 'segments_average_percentage' : segments_average_percentage , 'all_projects_average' : np . mean ( all_projects_percentages ) } )
Return the average percentage of new providers
    per segment and the average percentage of all projects.