- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 JSON,它正在从 http://api.worldweatheronline.com/ 发出请求。通过他们有用的 API。
我正在努力将 JSON 转换为表格格式,例如 pandas 数据框。我认为问题是由于 JSON 的嵌套结构造成的。
我已经尝试过pd.DataFrame(json)
,但是它的格式不正确,因为它与嵌套结构作斗争,因为“天气每小时时间”跨越许多行,而初始行跨越单行。
我还尝试过导出为 JSON 并读入为 pd.read_json
,但这也遇到了类似的问题。
非常感谢您的帮助!
JSON如下:
{'data': {'request': [{'type': 'UK Postcode', 'query': 'E4'}],
'weather': [{'date': '2018-11-28',
'astronomy': [{'sunrise': '07:39 AM',
'sunset': '03:57 PM',
'moonrise': '09:46 PM',
'moonset': '12:21 PM',
'moon_phase': 'Last Quarter',
'moon_illumination': '69'}],
'maxtempC': '13',
'maxtempF': '56',
'mintempC': '10',
'mintempF': '51',
'totalSnow_cm': '0.0',
'sunHour': '3.1',
'uvIndex': '0',
'hourly': [{'time': '0',
'tempC': '9',
'tempF': '48',
'windspeedMiles': '4',
'windspeedKmph': '7',
'winddirDegree': '212',
'winddir16Point': 'SSW',
'weatherCode': '296',
'weatherIconUrl': [{'value': 'http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0033_cloudy_with_light_rain_night.png'}],
'weatherDesc': [{'value': 'Light rain'}],
'precipMM': '1.6',
'humidity': '93',
'visibility': '11',
'pressure': '1010',
'cloudcover': '100',
'HeatIndexC': '9',
'HeatIndexF': '48',
'DewPointC': '8',
'DewPointF': '46',
'WindChillC': '8',
'WindChillF': '46',
'WindGustMiles': '8',
'WindGustKmph': '12',
'FeelsLikeC': '8',
'FeelsLikeF': '46'},
{'time': '300',
'tempC': '9',
'tempF': '48',
'windspeedMiles': '7',
'windspeedKmph': '11',
'winddirDegree': '174',
'winddir16Point': 'S',
'weatherCode': '266',
'weatherIconUrl': [{'value': 'http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0033_cloudy_with_light_rain_night.png'}],
'weatherDesc': [{'value': 'Light drizzle'}],
'precipMM': '0.5',
'humidity': '92',
'visibility': '15',
'pressure': '1009',
'cloudcover': '100',
'HeatIndexC': '9',
'HeatIndexF': '48',
'DewPointC': '8',
'DewPointF': '46',
'WindChillC': '7',
'WindChillF': '45',
'WindGustMiles': '11',
'WindGustKmph': '17',
'FeelsLikeC': '7',
'FeelsLikeF': '45'},
{'time': '600',
'tempC': '11',
'tempF': '51',
'windspeedMiles': '11',
'windspeedKmph': '18',
'winddirDegree': '175',
'winddir16Point': 'S',
'weatherCode': '266',
'weatherIconUrl': [{'value': 'http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0033_cloudy_with_light_rain_night.png'}],
'weatherDesc': [{'value': 'Light drizzle'}],
'precipMM': '0.7',
'humidity': '95',
'visibility': '14',
'pressure': '1008',
'cloudcover': '100',
'HeatIndexC': '11',
'HeatIndexF': '51',
'DewPointC': '10',
'DewPointF': '50',
'WindChillC': '8',
'WindChillF': '47',
'WindGustMiles': '19',
'WindGustKmph': '31',
'FeelsLikeC': '8',
'FeelsLikeF': '47'},
{'time': '900',
'tempC': '12',
'tempF': '54',
'windspeedMiles': '12',
'windspeedKmph': '19',
'winddirDegree': '208',
'winddir16Point': 'SSW',
'weatherCode': '296',
'weatherIconUrl': [{'value': 'http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0017_cloudy_with_light_rain.png'}],
'weatherDesc': [{'value': 'Light rain'}],
'precipMM': '0.8',
'humidity': '93',
'visibility': '14',
'pressure': '1008',
'cloudcover': '100',
'HeatIndexC': '12',
'HeatIndexF': '54',
'DewPointC': '11',
'DewPointF': '52',
'WindChillC': '10',
'WindChillF': '51',
'WindGustMiles': '20',
'WindGustKmph': '32',
'FeelsLikeC': '10',
'FeelsLikeF': '51'},
{'time': '1200',
'tempC': '13',
'tempF': '56',
'windspeedMiles': '16',
'windspeedKmph': '26',
'winddirDegree': '209',
'winddir16Point': 'SSW',
'weatherCode': '266',
'weatherIconUrl': [{'value': 'http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0017_cloudy_with_light_rain.png'}],
'weatherDesc': [{'value': 'Light drizzle'}],
'precipMM': '0.2',
'humidity': '87',
'visibility': '15',
'pressure': '1008',
'cloudcover': '100',
'HeatIndexC': '13',
'HeatIndexF': '56',
'DewPointC': '11',
'DewPointF': '52',
'WindChillC': '11',
'WindChillF': '52',
'WindGustMiles': '25',
'WindGustKmph': '41',
'FeelsLikeC': '11',
'FeelsLikeF': '52'},
{'time': '1500',
'tempC': '13',
'tempF': '56',
'windspeedMiles': '19',
'windspeedKmph': '31',
'winddirDegree': '205',
'winddir16Point': 'SSW',
'weatherCode': '266',
'weatherIconUrl': [{'value': 'http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0017_cloudy_with_light_rain.png'}],
'weatherDesc': [{'value': 'Light drizzle'}],
'precipMM': '0.5',
'humidity': '84',
'visibility': '15',
'pressure': '1007',
'cloudcover': '100',
'HeatIndexC': '13',
'HeatIndexF': '56',
'DewPointC': '11',
'DewPointF': '51',
'WindChillC': '11',
'WindChillF': '52',
'WindGustMiles': '30',
'WindGustKmph': '48',
'FeelsLikeC': '11',
'FeelsLikeF': '52'},
{'time': '1800',
'tempC': '10',
'tempF': '51',
'windspeedMiles': '19',
'windspeedKmph': '31',
'winddirDegree': '216',
'winddir16Point': 'SW',
'weatherCode': '122',
'weatherIconUrl': [{'value': 'http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png'}],
'weatherDesc': [{'value': 'Overcast'}],
'precipMM': '0.0',
'humidity': '82',
'visibility': '15',
'pressure': '1007',
'cloudcover': '100',
'HeatIndexC': '12',
'HeatIndexF': '54',
'DewPointC': '11',
'DewPointF': '51',
'WindChillC': '10',
'WindChillF': '51',
'WindGustMiles': '30',
'WindGustKmph': '49',
'FeelsLikeC': '10',
'FeelsLikeF': '51'},
{'time': '2100',
'tempC': '6',
'tempF': '44',
'windspeedMiles': '15',
'windspeedKmph': '23',
'winddirDegree': '219',
'winddir16Point': 'SW',
'weatherCode': '353',
'weatherIconUrl': [{'value': 'http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0025_light_rain_showers_night.png'}],
'weatherDesc': [{'value': 'Light rain shower'}],
'precipMM': '0.5',
'humidity': '82',
'visibility': '15',
'pressure': '1008',
'cloudcover': '70',
'HeatIndexC': '8',
'HeatIndexF': '47',
'DewPointC': '10',
'DewPointF': '50',
'WindChillC': '6',
'WindChillF': '44',
'WindGustMiles': '24',
'WindGustKmph': '39',
'FeelsLikeC': '6',
'FeelsLikeF': '44'}]}]}}
我想要的 csv 示例:
"request__type", "request__query", "weather__date", "weather__astronomy__sunrise", "weather__astronomy__sunset", "weather__astronomy__moonrise", "weather__astronomy__moonset", "weather__astronomy__moon_phase", "weather__astronomy__moon_illumination", "weather__maxtempC", "weather__maxtempF", "weather__mintempC", "weather__mintempF", "weather__totalSnow_cm", "weather__sunHour", "weather__uvIndex", "weather__hourly__time", "weather__hourly__tempC", "weather__hourly__tempF", "weather__hourly__windspeedMiles", "weather__hourly__windspeedKmph", "weather__hourly__winddirDegree", "weather__hourly__winddir16Point", "weather__hourly__weatherCode", "weather__hourly__weatherIconUrl__value", "weather__hourly__weatherDesc__value", "weather__hourly__precipMM", "weather__hourly__humidity", "weather__hourly__visibility", "weather__hourly__pressure", "weather__hourly__cloudcover", "weather__hourly__HeatIndexC", "weather__hourly__HeatIndexF", "weather__hourly__DewPointC", "weather__hourly__DewPointF", "weather__hourly__WindChillC", "weather__hourly__WindChillF", "weather__hourly__WindGustMiles", "weather__hourly__WindGustKmph", "weather__hourly__FeelsLikeC", "weather__hourly__FeelsLikeF"
"UK Postcode", "E4", "2018-11-28", "07:39 AM", "03:57 PM", "09:46 PM", "12:21 PM", "Last Quarter", "69", "13", "56", "10", "51", "0.0", "3.1", "0", "0", "9", "48", "4", "7", "212", "SSW", "296", "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0033_cloudy_with_light_rain_night.png", "Light rain", "1.6", "93", "11", "1010", "100", "9", "48", "8", "46", "8", "46", "8", "12", "8", "46"
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "300", "9", "48", "7", "11", "174", "S", "266", "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0033_cloudy_with_light_rain_night.png", "Light drizzle", "0.5", "92", "15", "1009", "100", "9", "48", "8", "46", "7", "45", "11", "17", "7", "45"
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "600", "11", "51", "11", "18", "175", "S", "266", "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0033_cloudy_with_light_rain_night.png", "Light drizzle", "0.7", "95", "14", "1008", "100", "11", "51", "10", "50", "8", "47", "19", "31", "8", "47"
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "900", "12", "54", "12", "19", "208", "SSW", "296", "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0017_cloudy_with_light_rain.png", "Light rain", "0.8", "93", "14", "1008", "100", "12", "54", "11", "52", "10", "51", "20", "32", "10", "51"
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "1200", "13", "56", "16", "26", "209", "SSW", "266", "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0017_cloudy_with_light_rain.png", "Light drizzle", "0.2", "87", "15", "1008", "100", "13", "56", "11", "52", "11", "52", "25", "41", "11", "52"
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "1500", "13", "56", "19", "31", "205", "SSW", "266", "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0017_cloudy_with_light_rain.png", "Light drizzle", "0.5", "84", "15", "1007", "100", "13", "56", "11", "51", "11", "52", "30", "48", "11", "52"
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "1800", "10", "51", "19", "31", "216", "SW", "122", "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png", "Overcast", "0.0", "82", "15", "1007", "100", "12", "54", "11", "51", "10", "51", "30", "49", "10", "51"
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "2100", "6", "44", "15", "23", "219", "SW", "353", "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0025_light_rain_showers_night.png", "Light rain shower", "0.5", "82", "15", "1008", "70", "8", "47", "10", "50", "6", "44", "24", "39", "6", "44"
最佳答案
我将你的 JSON 保存到一个文件中('test.json',顺便说一句,撇号需要用引号交换,以便 json
模块可以解析它)并使用 json
模块读取它。您希望最终得到一个 pandas DataFrame,我们需要 defaultdicts 来使扁平化变得容易:
import json
import pandas as pd
from collections import defaultdict
由于您将使用一个嵌套结构,该结构在每个新上下文中看起来都会有所不同,因此您首先需要一个函数来展平由多个 dict
和 list 组成的通用嵌套对象
直到你得到一个浅的dict
:我的建议:
def flatten_json(json):
flattened_dict = defaultdict(list)
def flatten(structure):
global key
if type(structure) is dict:
for key in structure:
flatten(structure[key])
elif type(structure) is list:
for item in structure:
flatten(item)
else:
flattened_dict[key].append(structure)
flatten(json)
return flattened_dict
快到了。我们唯一需要解决的问题是,如果所有 dict 值的长度相同,pandas 就只能从字典中构建 Dataframe。因此我们需要一个函数来将较短的值填充到最长的长度:
def fill_up_dict(dicti):
max_length = max([len(liist) for liist in dicti.values()])
for value in dicti.values():
remaining_length = max_length - len(value)
value.extend([""]*remaining_length)
return dicti
完成。只需读取您的 JSON,将其展平、填充并将其转储到 Dataframe 中即可:
with open('test.json', 'r') as f:
json_data = json.load(f)
flat_dict = flatten_json(json_data)
homogeneous_dict = fill_up_dict(flat_dict)
df = pd.DataFrame(homogeneous_dict)
出站 df
看起来如我们所愿:
DewPointC DewPointF FeelsLikeC FeelsLikeF HeatIndexC HeatIndexF WindChillC
0 8 46 8 46 9 48 8
1 8 46 7 45 9 48 7
2 10 50 8 47 11 51 8
3 11 52 10 51 12 54 10
4 11 52 11 52 13 56 11
5 11 51 11 52 13 56 11
6 11 51 10 51 12 54 10
7 10 50 6 44 8 47 6
8
9
10
11
12
13
14
15
WindChillF WindGustKmph WindGustMiles ... totalSnow_cm \
0 46 12 8 ... 0.0
1 45 17 11 ...
2 47 31 19 ...
3 51 32 20 ...
4 52 41 25 ...
5 52 48 30 ...
6 51 49 30 ...
7 44 39 24 ...
8 ...
9 ...
10 ...
11 ...
12 ...
13 ...
14 ...
15 ...
type uvIndex value \
0 UK Postcode 0 http://cdn.worldweatheronline.net/images/wsymb...
1 Light rain
2 http://cdn.worldweatheronline.net/images/wsymb...
3 Light drizzle
4 http://cdn.worldweatheronline.net/images/wsymb...
5 Light drizzle
6 http://cdn.worldweatheronline.net/images/wsymb...
7 Light rain
8 http://cdn.worldweatheronline.net/images/wsymb...
9 Light drizzle
10 http://cdn.worldweatheronline.net/images/wsymb...
11 Light drizzle
12 http://cdn.worldweatheronline.net/images/wsymb...
13 Overcast
14 http://cdn.worldweatheronline.net/images/wsymb...
15 Light rain shower
visibility weatherCode winddir16Point winddirDegree windspeedKmph \
0 11 296 SSW 212 7
1 15 266 S 174 11
2 14 266 S 175 18
3 14 296 SSW 208 19
4 15 266 SSW 209 26
5 15 266 SSW 205 31
6 15 122 SW 216 31
7 15 353 SW 219 23
8
9
10
11
12
13
14
15
windspeedMiles
0 4
1 7
2 11
3 12
4 16
5 19
6 19
7 15
8
9
10
11
12
13
14
15
[16 rows x 40 columns]
祝您的项目顺利!
D.
关于python - JSON 文件的表格格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53527688/
今天我在一个 Java 应用程序中看到了几种不同的加载文件的方法。 文件:/ 文件:// 文件:/// 这三个 URL 开头有什么区别?使用它们的首选方式是什么? 非常感谢 斯特凡 最佳答案 file
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我有一个 javascript 文件,并且在该方法中有一个“测试”方法,我喜欢调用 C# 函数。 c# 函数与 javascript 文件不在同一文件中。 它位于 .cs 文件中。那么我该如何管理 j
需要检查我使用的文件/目录的权限 //filePath = path of file/directory access denied by user ( in windows ) File fil
我在一个目录中有很多 java 文件,我想在我的 Intellij 项目中使用它。但是我不想每次开始一个新项目时都将 java 文件复制到我的项目中。 我知道我可以在 Visual Studio 和
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
我有 3 个组件的 Twig 文件: 文件 1: {# content-here #} 文件 2: {{ title-here }} {# content-here #}
我得到了 mod_ldap.c 和 mod_authnz_ldap.c 文件。我需要使用 Linux 命令的 mod_ldap.so 和 mod_authnz_ldap.so 文件。 最佳答案 从 c
我想使用PIE在我的项目中使用 IE7。 但是我不明白的是,我只能在网络服务器上使用 .htc 文件吗? 我可以在没有网络服务器的情况下通过浏览器加载的本地页面中使用它吗? 我在 PIE 的文档中看到
我在 CI 管道中考虑这一点,我应该首先构建和测试我的应用程序,结果应该是一个 docker 镜像。 我想知道使用构建环境在构建服务器上构建然后运行测试是否更常见。也许为此使用构建脚本。最后只需将 j
using namespace std; struct WebSites { string siteName; int rank; string getSiteName() {
我是 Linux 新手,目前正在尝试使用 ginkgo USB-CAN 接口(interface) 的 API 编程功能。为了使用 C++ 对 API 进行编程,他们提供了库文件,其中包含三个带有 .
我刚学C语言,在实现一个程序时遇到了问题将 test.txt 文件作为程序的输入。 test.txt 文件的内容是: 1 30 30 40 50 60 2 40 30 50 60 60 3 30 20
如何连接两个tcpdump文件,使一个流量在文件中出现一个接一个?具体来说,我想“乘以”一个 tcpdump 文件,这样所有的 session 将一个接一个地按顺序重复几次。 最佳答案 mergeca
我有一个名为 input.MP4 的文件,它已损坏。它来自闭路电视摄像机。我什么都试过了,ffmpeg , VLC 转换,没有运气。但是,我使用了 mediainfo和 exiftool并提取以下信息
我想做什么? 我想提取 ISO 文件并编辑其中的文件,然后将其重新打包回 ISO 文件。 (正如你已经读过的) 我为什么要这样做? 我想开始修改 PSP ISO,为此我必须使用游戏资源、 Assets
给定一个 gzip 文件 Z,如果我将其解压缩为 Z',有什么办法可以重新压缩它以恢复完全相同的 gzip 文件 Z?在粗略阅读了 DEFLATE 格式后,我猜不会,因为任何给定的文件都可能在 DEF
我必须从数据库向我的邮件 ID 发送一封带有附件的邮件。 EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Adventure Works Admin
我有一个大的 M4B 文件和一个 CUE 文件。我想将其拆分为多个 M4B 文件,或将其拆分为多个 MP3 文件(以前首选)。 我想在命令行中执行此操作(OS X,但如果需要可以使用 Linux),而
快速提问。我有一个没有实现文件的类的项目。 然后在 AppDelegate 我有: #import "AppDelegate.h" #import "SomeClass.h" @interface A
我是一名优秀的程序员,十分优秀!