JSON文件的表格格式

时间:2018-11-28 20:36:52

标签: python json pandas

我有一个JSON,它通过http://api.worldweatheronline.com/的有用API发出请求。

我正在努力将JSON转换为表格格式,例如pandas数据框。我认为问题是由于JSON的嵌套结构造成的。

我尝试过System.DllNotFoundException: 'Unable to load DLL 'mydll.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)' ,但是由于“天气每小时”跨越多行,而初始行跨越了单行,因此这种格式无法正确处理嵌套结构,因此无法正确格式化。

我还尝试了导出为JSON并以pd.DataFrame(json)的形式读取,但是这也遇到了类似的问题。

非常感谢您的帮助!

JSON如下:

pd.read_json

我想要的示例csv:

{'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'}]}]}}

1 个答案:

答案 0 :(得分:1)

我将您的JSON保存到了一个文件(顺便说一下,“ test.json”,其中的撇号需要换成逗号,以便json模块可以解析它)并读取它json模块。您想以pandas DataFrame结尾,我们将需要defaultdicts来使扁平化变得容易:

import json
import pandas as pd
from collections import defaultdict

由于您要使用的嵌套结构在每个新上下文中都会看起来有所不同,因此首先需要一个函数来展平由多个dictlist组成的常规嵌套对象,直到您使用得到浅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。