user4679594
user4679594

Reputation:

Python - Apply changes to entire column

What's the best way to modify all the values in an entire column?

This is what it looks like:

data.locations.head()    

0           [{'name': 'Chevy Chase, MD'}]
1                [{'name': 'Irvine, CA'}]
2    [{'name': 'San Francisco Bay Area'}]
3                 [{'name': 'Macon, GA'}]
4               [{'name': 'Fremont, CA'}]
Name: locations, dtype: object

I want to change it to this:

0           [{'Chevy Chase, MD'}]
1                [{'Irvine, CA'}]
2    [{'San Francisco Bay Area'}]
3                 [{'Macon, GA'}]
4               [{'Fremont, CA'}]

Upvotes: 2

Views: 100

Answers (1)

jezrael
jezrael

Reputation: 862511

You can use apply with selecting first dict in list by [0] and then key name:

print (df.locations.apply(lambda x: [{x[0]['name']}]))
0           [{Chevy Chase, MD}]
1                [{Irvine, CA}]
2    [{San Francisco Bay Area}]
3                 [{Macon, GA}]
4               [{Fremont, CA}]
Name: locations, dtype: object

Upvotes: 2

Related Questions