sectechguy
sectechguy

Reputation: 2117

Python Pandas dataframe strip a specific key out of dict inside list in column

I have a pandas dataframe called get_groups_df with several columns but I am having an issue with this particular column links. It seems that in this column that each row is a list with a dictionary inside Like the following:

get_groups_df.links[0:]
0    [{'action': 'GET', 'href': 'https://api.mysource.com/groups/asdfadsfa/users', 'type': 'application/json', 'rel': 'users'}]
1    [{'action': 'GET', 'href': 'https://api.mysource.com/groups/eweasdxcv/users', 'type': 'application/json', 'rel': 'users'}]
2    [{'action': 'GET', 'href': 'https://api.mysource.com/groups/aeasdfse/users', 'type': 'application/json', 'rel': 'users'}]
3    [{'action': 'GET', 'href': 'https://api.mysource.com/groups/asfesfsas/users', 'type': 'application/json', 'rel': 'users'}]

My goal is to just extract the 'href' portion so all that's left is the https... portion like this:

df['links']

  links
0 https://api.mysource.com/groups/asdfadsfa/users 
1 https://api.mysource.com/groups/eweasdxcv/users
2 https://api.mysource.com/groups/aeasdfse/users 
3 https://api.mysource.com/groups/asfesfsas/users

I know how to extract extract a single one like get_groups_df.links[0][0]['href'] and get_groups_df.links[1][0]['href'] would get the second one but this is specific and would like to apply this to the column. How can I accomplish this?

Upvotes: 1

Views: 174

Answers (1)

Ben.T
Ben.T

Reputation: 29635

you can use str accessor like:

get_groups_df['links'].str[0].str['href']

where the [0] means first element in your list for each row, not the row number

Or you could use explode to remove the list-like cell and then create dataframe from the result:

pd.DataFrame(get_groups_df['links'].explode().tolist())['href']

both gives

0    https://api.mysource.com/groups/asdfadsfa/users
1    https://api.mysource.com/groups/eweasdxcv/users
2     https://api.mysource.com/groups/aeasdfse/users
Name: href, dtype: object

The second solution could be usefull if you have more than one element in each list

Upvotes: 2

Related Questions