1

i have a list like this.

result =  [
{'Global': {'NewConfirmed': 100282, 'TotalConfirmed': 1162857, 'NewDeaths': 5658, 'TotalDeaths': 63263, 'NewRecovered': 15405, 'TotalRecovered': 230845}},
{'Global': {'NewConfirmed': 106598, 'TotalConfirmed': 1252421, 'NewDeaths': 5972, 'TotalDeaths': 67572, 'NewRecovered': 11066, 'TotalRecovered': 241599}}
]

I want to convert this data into dataframe as below.

   NewConfirmed  TotalConfirmed  NewDeaths  TotalDeaths  NewRecovered  TotalRecovered
0        100282         1162857       5658        63263         15405          230845
1        106598         1252421       5972        67572         11066          241599

tried many possibilites, but could not find a solution.

1 Answer 1

4

Use list comprehension for select by Global with DataFrame constructor:

df = pd.DataFrame([x['Global'] for x in result])
print (df)
   NewConfirmed  TotalConfirmed  NewDeaths  TotalDeaths  NewRecovered  \
0        100282         1162857       5658        63263         15405   
1        106598         1252421       5972        67572         11066   

   TotalRecovered  
0          230845  
1          241599  

If more keys in outer dictionaries use json.json_normalize:

from pandas.io.json import json_normalize

df = json_normalize(result)
print (df)
   Global.NewConfirmed  Global.TotalConfirmed  Global.NewDeaths  \
0               100282                1162857              5658   
1               106598                1252421              5972   

   Global.TotalDeaths  Global.NewRecovered  Global.TotalRecovered  
0               63263                15405                 230845  
1               67572                11066                 241599  
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot.. the second option i did get it, however, the header had key= global added to each filed which i was not looking for. Thanks a lot.
But why is that when we use a for loop instead of list comprehension, it throws error like raise ValueError("If using all scalar values, you must pass an index") ValueError: If using all scalar values, you must pass an index ? This is why i was not able to obtain desired result.
@VijayaBhaskar - then is possible use df.columns = df.columns.str.replace('Global.','') or df.columns = df.columns.str.split('.').str[1]
@VijayaBhaskar - Is possible edit question with your loop solution?

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.