0

I have a dictionary in a column dataframe, and I need to get a value from that

    title   tracklist
0   Life    [{'duration': '', 'position': '1', 'type_': 't...
1   Vuelve  [{'duration': '3:07', 'position': '1', 'type_'...
2   MTV Unplugged   [{'duration': '4:38', 'position': '1', 'type_'...
3   Ricky Martin    [{'duration': '4:03', 'position': '1', 'type_'...
4   Me Amaras   [{'duration': '3:30', 'position': '1', 'type_'...
5   A Medio Vivir   [{'duration': '5:30', 'position': '1', 'type_'...
6   Sound Loaded    [{'duration': '4:42', 'position': '1', 'type_'...
7   Ricky Martin    [{'duration': '4:13', 'position': 'A1', 'type_...
8   Musica + Alma + Sexo    [{'extraartists': [{'join': '', 'name': 'Rober...
9   Live Blanco Y Negro Tour    [{'extraartists': [{'join': '', 'name': 'David...
10  Almas Del Silencio  [{'duration': '3:43', 'position': '1', 'type_'...
11  A Quien Quiera Escuchar [{'duration': '4:00', 'position': '1', 'type_'...
12  Evita (New Broadway Cast Recording) [{'duration': '', 'position': '1-1'

, 'type_': ...

I need to extract the duration from the tracklist column, How to do it?

Alexa
  • 99
  • 1
  • 6
  • 3
    Welcome to SO. Please, try to post an example that we can actually use, such as a Python snippet with a data frame with a sample of your data, and indicate exactly what you want to get as output. If you have a data frame, say `df`, and want to get the `tracklist` column, usually you just need to do `df['tracklist']` or `df.tracklist`. – jdehesa Aug 06 '19 at 17:31
  • Thank you but I get this 0 nan 1 nan 2 nan 3 nan 4 nan 5 nan 6 nan 7 nan 10 nan 11 nan 12 nan Name: tracklist, dtype: object – Alexa Aug 06 '19 at 17:34
  • Echi\oing the comment above, please take a look at [How to create good, reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and give a [mcve] including input, output, and what you've tried so far – G. Anderson Aug 06 '19 at 17:35

2 Answers2

0

Lets assume you want the type_ of the Life title.

life_row = df.loc[df['Title']=='Life']

This will get us the row where the title = life, in a dataframe format.

life_row['tracklist'][0]['type_']

This will get us the type_ field. We need to index it by [0] because it's a dictionary nested in a list.

alex067
  • 3,159
  • 1
  • 11
  • 17
0

First we'll define a function that return the duration value from a dictionary contained in a list

def get_duration(row):
    return row[0]['duration']

then well apply this function to every element of your column using the map method

df['tracklist'].map(get_duration)
  • Thanks but I got this error: 6 def get_duration(row): ----> 7 return row[0]['duration'] 8 result3['tracklist'].map(get_duration) TypeError: string indices must be integers – Alexa Aug 06 '19 at 17:42
  • can you please share a sample df of your data so I may track the error – Djaballah Mohammed DJEDID Aug 06 '19 at 17:46
  • this is my df, with 2 columns title and tracklist. The problems is in tracklist because it is a dictionary in the column of the dataframe: title tracklist 0 Life [{'duration': '', 'position': '1', 'type_': 't... 1 Vuelve [{'duration': '3:07', 'position': '1', 'type_'... 2 MTV Unplugged [{'duration': '4:38', 'position': '1', 'type_'... 3 Ricky Martin [{'duration': '4:03', 'position': '1', 'type_'... 4 Me Amaras [{'duration': '3:30', 'position': '1', 'type_'... 5 A Medio Vivir [{'duration': '5:30', 'position': '1', 'type_'... . – Alexa Aug 06 '19 at 17:49
  • can you do df.head(), take a screenshot of the result and share it – Djaballah Mohammed DJEDID Aug 06 '19 at 17:56
  • can you do df.head(), take a screenshot of the result and share it, because df['tracklist'] seems to be a list of dictionaries, and if that's the case my code should work – Djaballah Mohammed DJEDID Aug 06 '19 at 18:04
  • I am very new in Stackoverflow and I am not allowed to share images. I have a df with 2 columns: title with names of songs (Life, Vuelve, MTV Unplugged..) and another column with tracklist {duration, position..) – Alexa Aug 06 '19 at 18:05