当前位置: 首页 > 文档资料 > Python 数据科学 >

Python Processing JSON Data

优质
小牛编辑
132浏览
2023-12-01

JSON文件以人类可读的格式将数据存储为文本。 JSON代表JavaScript Object Notation。 Pandas可以使用read_json函数读取JSON文件。

输入数据 (Input Data)

通过将以下数据复制到文本编辑器(如记事本)来创建JSON文件。 使用.json扩展名保存文件,并选择文件类型作为all files(*.*)

{ 
   "ID":["1","2","3","4","5","6","7","8" ],
   "Name":["Rick","Dan","Michelle","Ryan","Gary","Nina","Simon","Guru" ]
   "Salary":["623.3","515.2","611","729","843.25","578","632.8","722.5" ],
   "StartDate":[ "1/1/2012","9/23/2013","11/15/2014","5/11/2014","3/27/2015","5/21/2013",
      "7/30/2013","6/17/2014"],
   "Dept":[ "IT","Operations","IT","HR","Finance","IT","Operations","Finance"]
}

阅读JSON文件

pandas库的read_json函数可用于将JSON文件读入pandas DataFrame。

import pandas as pd
data = pd.read_json('path/input.json')
print (data)

当我们执行上面的代码时,它会产生以下结果。

         Dept  ID    Name  Salary   StartDate
0          IT   1    Rick  623.30    1/1/2012
1  Operations   2     Dan  515.20   9/23/2013
2          IT   3   Tusar  611.00  11/15/2014
3          HR   4    Ryan  729.00   5/11/2014
4     Finance   5    Gary  843.25   3/27/2015
5          IT   6   Rasmi  578.00   5/21/2013
6  Operations   7  Pranab  632.80   7/30/2013
7     Finance   8    Guru  722.50   6/17/2014

阅读特定的列和行

与我们在上一章中已经看到的读取CSV文件类似,在将JSON文件读取到DataFrame之后,pandas库的read_json函数也可用于读取某些特定列和特定行。 为此,我们使用名为.loc()的多轴索引方法。 我们选择显示某些行的Salary和Name列。

import pandas as pd
data = pd.read_json('path/input.xlsx')
# Use the multi-axes indexing funtion
print (data.loc[[1,3,5],['salary','name']])

当我们执行上面的代码时,它会产生以下结果。

   salary   name
1   515.2    Dan
3   729.0   Ryan
5   578.0  Rasmi

将JSON文件读取为记录

我们还可以将to_json函数与参数一起应用,以将JSON文件内容读取到单个记录中。

import pandas as pd
data = pd.read_json('path/input.xlsx')
print(data.to_json(orient='records', lines=True))

当我们执行上面的代码时,它会产生以下结果。

{"Dept":"IT","ID":1,"Name":"Rick","Salary":623.3,"StartDate":"1\/1\/2012"}
{"Dept":"Operations","ID":2,"Name":"Dan","Salary":515.2,"StartDate":"9\/23\/2013"}
{"Dept":"IT","ID":3,"Name":"Tusar","Salary":611.0,"StartDate":"11\/15\/2014"}
{"Dept":"HR","ID":4,"Name":"Ryan","Salary":729.0,"StartDate":"5\/11\/2014"}
{"Dept":"Finance","ID":5,"Name":"Gary","Salary":843.25,"StartDate":"3\/27\/2015"}
{"Dept":"IT","ID":6,"Name":"Rasmi","Salary":578.0,"StartDate":"5\/21\/2013"}
{"Dept":"Operations","ID":7,"Name":"Pranab","Salary":632.8,"StartDate":"7\/30\/2013"}
{"Dept":"Finance","ID":8,"Name":"Guru","Salary":722.5,"StartDate":"6\/17\/2014"}