Access list element inside dataframe

267 Views Asked by At

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

1

There are 1 best solutions below

0
j__carlson On BEST ANSWER

No need for the .values.tolist(), try this:

names = []
for x in df['venue.categories']:
  n = x[0]['pluralName']
  names.append(n)

Besides the extra square brackets your object is a dictionary which can be called by x['dict_entry_name']. In this case x[0] gets rid of the square brackets and ['pluralName'] selects the entry.


Example:

ind=[0,1,2]

venue =[
[{'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'}],
[{'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/arts_entertainment/performingarts_theater_',
   'suffix': '.png'},
  'id': '4bf58dd8d48988d137941735',
  'name': 'Mall',
  'pluralName': 'Malls',
  'primary': True,
  'shortName': 'Mall'}],
[{'icon': {'prefix': 'https://ss3.4sqi.net/img/categories_v2/arts_entertainment/performingarts_theater_',
   'suffix': '.png'},
  'id': '4bf58dd8d48988d137941735',
  'name': 'Auditorium',
  'pluralName': 'Auditoriums',
  'primary': True,
  'shortName': 'Auditorium'}]    
]

df= pd.DataFrame({'col1':ind, 'venue.catagories':venue})
names=[]
for x in df['venue.catagories']:
  n = x[0]['pluralName']
  names.append(n)

Output:

['Theaters', 'Malls', 'Auditoriums']