[英]Struggling to make a nested json API ouput into a pandas df
我在 Python(API 輸出)中第一次使用 json 數據。 我有點難以理解如何將以下結果轉換為類似熊貓的 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,“濕度”:91},“能見度”:10000,“風”:{“速度”:5.81,“度”:119,“陣風”:7.15},“雲”:{“所有”:20} , 'dt': 1669622280, 'sys': {'type': 2, 'id': 2011538, 'country': 'DE', 'sunrise': 1669618193, 'sunset': 1669647541}, 'timezone': 3600 , 'id': 2950159, 'name': 'Berlin', 'cod': 200}
任何提示將不勝感激。 謝謝你。
我試過 pd.readDataframe, pd.read_json
注意:不要在下一個問題中將您的數據作為圖片共享。 如果您將其作為文本共享,我們可以輕松復制和粘貼它。 您現在可以使用它。
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 |
'''
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.