[英]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.