Imu Imu - 1 year ago 68
Python Question

Access values for a dictionary within a list in a column of a pandas dataframe

I have a column in a pandas dataframe, where each row is a list with a dictionary inside, like this:

[{'url':, 'expanded_url':}]
[{'url':, 'expanded_url':}]

Some of the rows only have an empty list.
So I'm trying to extract the value of just the expanded_url, and when I test the following function on a test list, I'm able to do that:

test_list = [{'url': '', 'expanded_url': ''}]

def get_expanded_url(outterlist):
for item in outterlist:
if isinstance(item, dict):
return item['expanded_url']
return None

However when I apply this to a column in the dataframe like this:

df.urls.apply(lambda x: get_expanded_url(x))

I only get NaNs, even where I shouldn't (where there isn't just an empty list). First of all can someone explain why my function doesn't work on the dataframe? And secondly, how can I extract just the values for expand_url from the column?

Answer Source

You can try this:

def get_expanded_url(outterlist):
        return outterlist[0]['expanded_url']
    except IndexError:
        return None


The function will try to obtain the url that you want. If it can't, it will return None.

Also, when using apply, you can give only the name of the function. It is not required to create the lambda function.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download