Access values for a dictionary within a list in a column of a pandas dataframe
<p>I have a column in a pandas dataframe, where each row is a list with a dictionary inside, like this:</p>

<pre><code>urls
---------------------------------------------------------
[{'url': http://t.co, 'expanded_url':http://nytimes.com}]
[{'url': http://t.co, 'expanded_url':http://time.com}] 
[]
</code></pre>

<p>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:</p>

<pre><code>test_list = [{'url': 'https://t.co', 'expanded_url': 'https://nytimes.com'}]

def get_expanded_url(outterlist):
    for item in outterlist:
        if isinstance(item, dict):
            return item['expanded_url']
        else:
            return None
</code></pre>

<p>However when I apply this to a column in the dataframe like this:</p>

<pre><code>df.urls.apply(lambda x: get_expanded_url(x))
</code></pre>

<p>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?</p>

<p>You can try this:</p>

<pre><code>def get_expanded_url(outterlist):
    try:
        return outterlist[0]['expanded_url']
    except IndexError:
        return None

df.urls.apply(get_expanded_url)
</code></pre>

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

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

python pandas