Home > other >  pandas split list like object
pandas split list like object

Time:09-25

Hi I have this column of data named labels:

    [{'id': 123456,
      'name': John,
      'age': 22,
      'pet': None,
      'gender': male,
      'result': [{'id': 'vEo0PIYPEE',
        'type': 'choices',
        'value': {'choices': ['Same Person']},
        'to_name': 'image',
        'from_name': 'person_evaluation'}]}]

    [{'id': 123457,
      'name': May,
      'age': 21,
      'pet': None,
      'gender': female,
      'result': [{'id': zTHYuKIOQ',
        'type': 'choices',
        'value': {'choices': ['Different Person']},
        'to_name': 'image',
        'from_name': 'person_evaluation'}]}]
......

Not sure what type is this, and I would like to break this down, to extract the value [Same Person], the outcome should be something like this:

0                      [Same Person]
1                      [Different Person]
....

How should I achieve this?

CodePudding user response:

Based on the limited data that you have provided, would this work?

df['labels_new'] = df['labels'].apply(lambda x: x[0].get('result')[0].get('value').get('choices'))
                                              labels          labels_new
0  [{'id': 123456, 'name': 'John', 'age': 22, 'pe...       [Same Person]
1  [{'id': 123457, 'name': 'May', 'age': 21, 'pet...  [Different Person]

You can use the following as well, but I find dict.get() to be more versatile (returning default values for example) and has better exception handling.

df['labels'].apply(lambda x: x[0]['result'][0]['value']['choices'])

You could consider using pd.json_normalize (read more here) but for the current state of your column that you have, its going to be a bit complex to extract the data with that, rather than simply using a lambda function

  • Related