python pandas:块状大小如何工作?

发布于 2025-01-31 07:50:37 字数 882 浏览 2 评论 0 原文

我有以下代码:

from numpy import dtype
import pandas as pd
import os
import sys

inputFile='data.json'
chunks = pd.read_json(inputFile, lines=True, chunksize = 1000)
original_stdout = sys.stdout

i = 1

for c in chunks:
    location = c.location.str.split(',')
    for b in range(1000):
        print(location[b])
        if not type(location[b]) == float:
            # get the country name
            country = location[b][-1]
        else:
            country = 'unknown'

我正在从包括JSON对象的大文件中提取位置字段。因为文件太大,所以我将其分为1000行的块。我循环浏览每个块并检索所需的信息:

for c in chunks:
    location = c.a.str.split(',')
    for b in range(1000):
        print(location[b])

在第一次迭代期间,一切都顺利进行。在第二次迭代中,行:

print(location[b])

给出错误:

ValueError: 0 is not in range

如何在第一个之后循环凹槽?

感谢您的帮助

I have the following code:

from numpy import dtype
import pandas as pd
import os
import sys

inputFile='data.json'
chunks = pd.read_json(inputFile, lines=True, chunksize = 1000)
original_stdout = sys.stdout

i = 1

for c in chunks:
    location = c.location.str.split(',')
    for b in range(1000):
        print(location[b])
        if not type(location[b]) == float:
            # get the country name
            country = location[b][-1]
        else:
            country = 'unknown'

I'm extracting the location field from a large file including json objects. Because the file is so large, I've divided it into 1000-line chunks. I cycle through each chunk and retrieve the information I require:

for c in chunks:
    location = c.a.str.split(',')
    for b in range(1000):
        print(location[b])

All goes smoothly during the first iteration. At the second iteration the line:

print(location[b])

gives the error:

ValueError: 0 is not in range

How do I cycle trough the chuncks following the first?

Thank you for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把梦留给海 2025-02-07 07:50:37

问题是,通过做位置[B] ,您正在访问 indote frame index (即,您在这里要求使用该行索引值 b )。这些块将正确遵循索引,这意味着第一个块将以 0 开始,第二个块由 1000 ,等等。这意味着,索引 0 仅包含在第一个块中。

因此,相反,您需要在没有索引的情况下迭代行:

for row in location:
   # Do something.

实际上,如果您查看错误的完整跟踪,您还将在 value> value> value> value> value> keyError >。

要迭代系列并具有索引,您可以使用

for idx, row in a.iteritems():
   # Do something...

The problem is that by doing location[b] you are accessing the location frame by index (i.e., here you are asking for the row with the index value b). The chunks will follow the index correctly, which means the first chunk will have the index starting by 0, the second by 1000, and so on. This means, index 0 will only be contained in the first chunk.

So, instead, you need to iterate the rows without the index:

for row in location:
   # Do something.

In fact, probably if you look at the full trace of the error you will also see a KeyError below the ValueError.

To iterate the Series and have the index you can use Series.iteritems():

for idx, row in a.iteritems():
   # Do something...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文