Struggling to make a nested json API ouput into a pandas df

Question:

I am working with json data for the first time in Python (API output). I am struggling a bit to understand how to convert the following results to a pandas-like dataframe:

{‘coord’: {‘lon’: 13.4105, ‘lat’: 52.5244}, ‘weather’: [{‘id’: 801, ‘main’: ‘Clouds’, ‘description’: ‘few clouds’, ‘icon’: ’02d’}], ‘base’: ‘stations’, ‘main’: {‘temp’: 3.21, ‘feels_like’: -1.29, ‘temp_min’: 2.22, ‘temp_max’: 4.09, ‘pressure’: 1007, ‘humidity’: 91}, ‘visibility’: 10000, ‘wind’: {‘speed’: 5.81, ‘deg’: 119, ‘gust’: 7.15}, ‘clouds’: {‘all’: 20}, ‘dt’: 1669622280, ‘sys’: {‘type’: 2, ‘id’: 2011538, ‘country’: ‘DE’, ‘sunrise’: 1669618193, ‘sunset’: 1669647541}, ‘timezone’: 3600, ‘id’: 2950159, ‘name’: ‘Berlin’, ‘cod’: 200}

Specifically, I would like that the data looked a bit like this:

Any tip would be greatly appreciated. Thank you.

I tried pd.readDataframe, pd.read_json

Asked By: alessia

||

Answers:

Note: Do not share your data as a picture in your next questions. If you share it as a text, we can easily copy and paste it. You can use this for now.

a={'coord': {'lon': 13.4105, 'lat': 52.5244}, 'weather': [{'id': 741, 'main': 'Fog', 'description': 'fog', 'icon': '50d'}], 'bas e': 'stations', 'main': {'temp': 5.95, 'feels like': 4.42, 'temp_min': 4.98, 'temp_max': 8.32, 'pressure': 1006, 'humidity': 93}, 'visibility': 750, 'wind': {'speed': 2.06, 'deg': 70}, 'clouds': {'all': 20}, 'dt': 1669385464, 'sys': {'type': 2, 'id': 2011538, 'country': 'DE', 'sunrise': 1669358707, 'sunset': 1669388510}, 'timezone': 3600, 'id': 2950159, 'name': 'Berlin', 'cod': 200}


df=pd.json_normalize(a,record_path='weather',record_prefix='weather_').join(pd.json_normalize(a)).drop(['weather'],axis=1)
print(df)
'''
|    |   weather_id | weather_main   | weather_description   | weather_icon   | bas e    |   visibility |         dt |   timezone |      id | name   |   cod |   coord.lon |   coord.lat |   main.temp |   main.feels like |   main.temp_min |   main.temp_max |   main.pressure |   main.humidity |   wind.speed |   wind.deg |   clouds.all |   sys.type |   sys.id | sys.country   |   sys.sunrise |   sys.sunset |
|---:|-------------:|:---------------|:----------------------|:---------------|:---------|-------------:|-----------:|-----------:|--------:|:-------|------:|------------:|------------:|------------:|------------------:|----------------:|----------------:|----------------:|----------------:|-------------:|-----------:|-------------:|-----------:|---------:|:--------------|--------------:|-------------:|
|  0 |          741 | Fog            | fog                   | 50d            | stations |          750 | 1669385464 |       3600 | 2950159 | Berlin |   200 |     13.4105 |     52.5244 |        5.95 |              4.42 |            4.98 |            8.32 |            1006 |              93 |         2.06 |         70 |           20 |          2 |  2011538 | DE            |    1669358707 |   1669388510 |
'''
Answered By: Clegane
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.