How to match pandas column of lists to a dictionary

  Kiến thức lập trình

I have survey results that have output numbers rather than the countries they correspond to.

So I have a data frame like this:

company geography
ABC [1,3,5]
DEF [8,9,12]

I have created a dict like this:

geog_dict = {
                '1': 'Australia',
                '2': 'Africa',
                '3': 'Canada',
                '4': 'Central Asia',
                '5': 'China',
                '6': 'East Asia',
                '7': 'Europe',
                '8': 'India',
                '9': 'Latin America & Caribbean',
                '10': 'Middle East & North Africa',
                '11': 'South Asia',
                '12': 'Southeast Asia',
                '13': 'United States'
}

I want to do something along the lines of,

df['geography'] = [geog_dict[x] for x in df['geography']]

I have googled around and looked at a number of other stack overflow problems but none are specifically this problem.

What’s the simpliest way to get this:

company geography
ABC [Australia, Canada, China]
DEF [India, Latin America & Caribbean, Southeast Asia]

LEAVE A COMMENT