Skip to content Skip to sidebar Skip to footer

Add Numeric Values Beside Columns Elements In Pandas

This is further part of question which I asked here . So I have decided to put it as another question. Is there any way so that I can add the relevance value beside each matched

Solution 1:

Use:

movies=['spiderman','marvels','thriller']
sports=['baseball','hockey','football']
politics=['election','china','usa']
d = {'movies':movies, 'sports':sports, 'politics':politics}
d1 = {k: oldk for oldk, oldv in d.items() for k in oldv}

def f(x):
    a = Counter([d1.get(y, 'miscellaneous') for y in x])
    return ', '.join(['{} {}'.format(k, v / sum(a.values())* 100 ) for k, v in a.items()])

df['matched_list_names'] = df['word_list'].apply(f)
print (df)
                            word_list  \
0  [nuclear, election, usa, baseball]   
1        [football, united, thriller]   
2     [marvels, hollywood, spiderman]   

                                  matched_list_names  
0     miscellaneous 25.0, politics 50.0, sports 25.0  
1  sports 33.33333333333333, miscellaneous 33.333...  
2  movies 66.66666666666666, miscellaneous 33.333...  

Post a Comment for "Add Numeric Values Beside Columns Elements In Pandas"