| 
                            
                                  前言本文讲解如何加载json文件或字符串为pandas数据框。pandas把json数据分成几种典型类型,希望对你实际数据应用开发有所启示。 有时可能需要转换json文件位pandas数据框。使用pandas内置的read_json()函数很容易实现, 其语法如下: 
read_json(‘path’, orient=’index’) 
	path: json文件的路径orient: json文件的格式描述,缺省是index,还有其他选型:split, records, columns, values。 下面通过几个示例进行说明。 records格式假设json文件my_file.json的格式如下: 
	
		
			| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | [    {       "points": 25,       "assists": 5    },    {       "points": 12,       "assists": 7    },    {       "points": 15,       "assists": 7    },    {       "points": 19,       "assists": 12    } ] |  我们使用pandas的函数read_json,只要只从orient参数位records: 
	
		
			| 1 2 3 4 5 | # 加载json文件,生成pandas数据框 df = pd.read_json('data/json_file.json', orient='records')   # 查看数据框 print(df) |  输出结果: 
   points  assists0      25        5
 1      12        7
 2      15        7
 3      19       12
 index格式假设json文件格式为: 
	
		
			| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | {    "0": {       "points": 25,       "assists": 5    },    "1": {       "points": 12,       "assists": 7    },    "2": {       "points": 15,       "assists": 7    },    "3": {       "points": 19,       "assists": 12    } } |  与上面实现代码一样,仅需要修改orient=‘index’: 
	
		
			| 1 2 3 4 | import pandas as pd   df = pd.read_json("data/my_file.json", orient='index') print(df) |  输出结果: 
   points  assists0      25        5
 1      12        7
 2      15        7
 3      19       12
 columns 类型假设json文件格式为: 
	
		
			| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | {    "points": {       "0": 25,       "1": 12,       "2": 15,       "3": 19    },    "assists": {       "0": 5,       "1": 7,       "2": 7,       "3": 12    } } |  加载代码修改orient参数为’columns’: 
 
	
		
			| 1 2 3 4 5 | import pandas as pd   df = pd.read_json("data/my_file.json", orient='columns')   print(df) |  结果与上面一致。 values格式假设json文件代码如下: 
	
		
			| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | [    [       25,       5    ],    [       12,       7    ],    [       15,       7    ],    [       19,       12    ] ] |  加载代码如下: 
	
		
			| 1 2 3 4 5 | import pandas as pd   df = pd.read_json("data/my_file.json", orient='values')   print(df) |  输出结果: 
    0   10  25   5
 1  12   7
 2  15   7
 3  19  12
 split 参数示例下面看split参数示例: 
	
		
			| 1 2 3 4 5 6 7 | import pandas as pd   # 示例数据 data =  '{"columns":["col 1","col 2"], "index":["row 1","row 2"], "data":[["a","b"],["c","d"]]}' df = pd.read_json(data, orient='split')   print(df) |  输出交叉表形式结果: 
      col 1 col 2row 1     a     b
 row 2     c     d
 如果不指定index,则行自动生成序号: 
	
		
			| 1 2 3 4 5 6 | import pandas as pd   data =  '{"columns":["col 1","col 2"],  "data":[["a","b"],["c","d"]]}' df = pd.read_json(data, orient='split')   print(df) |  输出结果: 
  col 1 col 20     a     b
 1     c     d
 压缩与编码使用compression参数可以解压并载入json文件,参数选型有:‘zip’, ‘gzip’, ‘bz2’, ‘zstd’。如果指定zip,则确保文件为zip文件格式,None表示不解压。 使用 encoding 指定自定义编码,缺省为 UTF-8 编码。 假设my_file.zip压缩文件格式为: 
	
		
			| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | [    [       25,       5    ],    [       12,       7    ],    [       15,       7    ],    [       19,       12    ] ] |  载入代码: 
	
		
			| 1 2 3 | import pandas as pd df = pd.read_json("data/my_file.zip", orient='values', compression='zip') print(df) |  
 |