'Extracting values from a list of dictionary in dataframe column and and create new column with

I have a dataframe column named 'urls' containing a list of dictionary in each row. I want to get a value of specific key in a dictionary and make new column having that value for each row. For example, the row-index 0 of the 'url' column

df['urls'][0]

prints as below.

[{"start": 92, "end": 115, "expanded_url": "https://www.instagram.com/p/Bevk4X-FQsI/", "display_url": "instagram.com/p/Bevk4X-FQsI/"}]

I want to make a new column named 'expanded' and the row-index 0 should have

df['expanded'][0]
"https://www.instagram.com/p/Bevk4X-FQsI/"

So far I have tried

df['expanded'] = df['urls'].str[0].str.get('expanded_url')

this prints

df['expanded'][0]
NaN

and

df['urls'].str[0]

prints

[

I think something is wrong but do not understand where I am wrong.

Could anyone help here to fix it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source