如何读大熊猫中的大型json?

时间:2017-10-17 12:41:18

标签: python json pandas

我的代码是:review 我将数据{ // string, 22 character unique review id "review_id": "zdSx_SD6obEhz9VrW9uAWA", // string, 22 character unique user id, maps to the user in user.json "user_id": "Ha3iJu77CxlrFm-vQRs_8g", // string, 22 character business id, maps to business in business.json "business_id": "tnhfDv5Il8EaGSXZGiuQGg", // integer, star rating "stars": 4, // string, date formatted YYYY-MM-DD "date": "2016-03-09", // string, the review itself "text": "Great place to hang out after work: the prices are decent, and the ambience is fun. It's a bit loud, but very lively. The staff is friendly, and the food is good. They have a good selection of drinks.", // integer, number of useful votes received "useful": 0, // integer, number of funny votes received "funny": 0, // integer, number of cool votes received "cool": 0 } 设为fllow:

    333             fh, handles = _get_handle(filepath_or_buffer, 'r',
    334                                       encoding=encoding)
--> 335             json = fh.read()
    336             fh.close()
    337         else:

OSError: [Errno 22] Invalid argument

但是我收到了以下错误:

import json
with open('review.json') as json_file:
    data = json.load(json_file)

我的jsonfile不包含任何评论和3.8G! 我只是从这里下载文件来练习link

当我使用以下代码时,抛出相同的错误:

transform-origin: calc(100% - #{$hit-area} / 2) ($hit-area / 2);

4 个答案:

答案 0 :(得分:5)

也许,您正在阅读的文件包含多个json对象,而不是方法json.load(json_file)pd.read_json('review.json')所期望的单个json或数组对象。这些方法应该用单个json对象读取文件。

从我看到的yelp数据集中,您的文件必须包含以下内容:

{"review_id":"xxxxx","user_id":"xxxxx","business_id":"xxxx","stars":5,"date":"xxx-xx-xx","text":"xyxyxyxyxx","useful":0,"funny":0,"cool":0}
{"review_id":"yyyy","user_id":"yyyyy","business_id":"yyyyy","stars":3,"date":"yyyy-yy-yy","text":"ababababab","useful":0,"funny":0,"cool":0}
....    
....

and so on.

因此,重要的是要意识到这不是单个json数据,而是一个文件中的多个json对象。

要将此数据读入pandas数据框,以下解决方案应该有效:

import pandas as pd

with open('review.json') as json_file:      
    data = json_file.readlines()
    # this line below may take at least 8-10 minutes of processing for 4-5 million rows. It converts all strings in list to actual json objects. 
    data = list(map(json.loads, data)) 

pd.DataFrame(data)

假设数据的大小非常大,我认为您的计算机将花费大量时间将数据加载到数据框中。

答案 1 :(得分:3)

如果您不想使用for循环,则应使用以下方法:

import pandas as pd

df = pd.read_json("foo.json", lines=True)

这将处理您的json文件看起来与此类似的情况:

{"foo": "bar"}
{"foo": "baz"}
{"foo": "qux"}

并将其转换为一个由单列foo和三行组成的DataFrame。

您可以在熊猫的docs

中阅读更多内容

答案 2 :(得分:1)

使用 arglines=True 和 chunksize=X 将创建一个读取器来获取特定行数。

然后你必须做一个循环来显示每个块。

这里有一段代码供您理解:

import pandas as pd
import json
chunks = pd.read_json('../input/data.json', lines=True, chunksize = 10000)
for chunk in chunks:
    print(chunk)
    break

Chunks 根据您的 json 的长度(按行说话)创建多个块。 例如,我有一个 100 000 行的 json,其中包含 X 个对象,如果我执行 chunksize = 10 000,我将有 10 个块。

在我给出的代码中,我添加了一个中断,以便只打印第一个块,但如果删除它,您将一个接一个地打印 10 个块。

答案 3 :(得分:0)

如果您的 json 文件包含多个对象而不是一个对象,则应执行以下操作:

import json

data = []
for line in open('sample.json', 'r'):
    data.append(json.loads(line))

注意 json.loadjson.loads 之间的区别。

json.loads() 需要一个(有效的)JSON 字符串 - 即 {"foo": "bar"}。因此,如果您的 json 文件看起来像@Mant1c0r3 提到的那样,那么 json.loads 将是合适的。

相关问题