1

I have a variable <class 'list'> that comes from a post request API in GraphQL that looks like this:

  [
    {'duration': 0, 
     'phase': 
        {
            'name': 'Start form'
        }
    }, 
    {'duration': 441, 
     'phase': 
        {
            'name': 'Análise do SAC - Mesas'
        }
    }, 
    {'duration': 126057, 
     'phase': 
        {
            'name': 'Análise do SAC - Industrial'
        }
    }
 ]

How can I iterate thru this list and create a dataframe that looks like this:

Start form Análise do SAC - Mesas Análise do SAC - Industrial
0 441 126057

I need to create the df like this because I will merge with another df

Also, I'm curious, what's the need of this list organization? Like a list of lists? List of dict?

Thanks in advance!!

2
  • 1
    "what's the need"? Do you mean "what's the name"? It's a list of dictionaries. Commented Dec 31, 2021 at 4:14
  • yes, that what i meant, thanks Commented Dec 31, 2021 at 17:32

2 Answers 2

2

Consider the following code which will produce the output you want:

import pandas as pd

response =  [
    {'duration': 0, 
     'phase': 
        {
            'name': 'Start form'
        }
    }, 
    {'duration': 441, 
     'phase': 
        {
            'name': 'Análise do SAC - Mesas'
        }
    }, 
    {'duration': 126057, 
     'phase': 
        {
            'name': 'Análise do SAC - Industrial'
        }
    }
 ]
 
data = {}
for obj in response:
    data[obj["phase"]["name"]] = [obj["duration"]]
     
df = pd.DataFrame(data)
print(df.to_string(index=False))
Sign up to request clarification or add additional context in comments.

Comments

1

Use:

df = pd.json_normalize(l)
df = df.set_index('phase.name').T.reset_index(drop=True)

OUTPUT

phase.name  Start form  Análise do SAC - Mesas  Análise do SAC - Industrial
0                    0                     441                       126057

If want to rename axis you can then use:

df.rename_axis(None, axis=1, inplace=True)

OUTPUT

   Start form  Análise do SAC - Mesas  Análise do SAC - Industrial
0           0                     441                       126057

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.