I have a data frame (df) with a column named venue.cateogries that is populated with information that looks like this:
[{'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/arts_entertainment/performingarts_theater_',
'suffix': '.png'},
'id': '4bf58dd8d48988d137941735',
'name': 'Theater',
'pluralName': 'Theaters',
'primary': True,
'shortName': 'Theater'}]
There are ~100 rows in the data frame and I'm trying to loop through it and get the value of 'pluralName' in each row.
Here is the loop I wrote to get it
category_names = df['venue.categories'].values.tolist()
names = []
for x in category_names:
n = category_names[x][0]['pluralName']
names.append(n)
This is giving me a TypeError: list indices must be integers or slices, not list
No need for the
.values.tolist(), try this:Besides the extra square brackets your object is a dictionary which can be called by
x['dict_entry_name']. In this casex[0]gets rid of the square brackets and['pluralName']selects the entry.Example:
Output: