Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用chunksize迭代获取推断的数据帧类型_Python_Type Conversion_Pandas_Hdfstore - Fatal编程技术网

Python 使用chunksize迭代获取推断的数据帧类型

Python 使用chunksize迭代获取推断的数据帧类型,python,type-conversion,pandas,hdfstore,Python,Type Conversion,Pandas,Hdfstore,如何使用pd.read_csv()以迭代方式对文件进行分块并 保留数据类型和其他元信息,就像我阅读整个文档一样 立即收集数据集 我需要读取一个数据集,该数据集太大,无法放入内存。我想使用pd.read_csv导入该文件,然后立即将该块附加到HDFStore中。然而,数据类型推断对后续块一无所知 如果表中存储的第一个区块仅包含int,而后续区块包含float,则会引发异常。因此,我需要首先使用read_csv遍历数据帧,并保留最高的推断类型。此外,对于对象类型,我需要保留最大长度,因为它们将作为字

如何使用pd.read_csv()以迭代方式对文件进行分块并 保留数据类型和其他元信息,就像我阅读整个文档一样 立即收集数据集

我需要读取一个数据集,该数据集太大,无法放入内存。我想使用pd.read_csv导入该文件,然后立即将该块附加到HDFStore中。然而,数据类型推断对后续块一无所知

如果表中存储的第一个区块仅包含int,而后续区块包含float,则会引发异常。因此,我需要首先使用read_csv遍历数据帧,并保留最高的推断类型。此外,对于对象类型,我需要保留最大长度,因为它们将作为字符串存储在表中


有没有一种泛音速的方法可以只保留这些信息而不读取整个数据集?

我不认为这会如此直观,否则我就不会发布这个问题了。但熊猫再一次让事情变得轻松起来。但是,将问题保留为这些信息可能对处理大型数据的其他人有用:

In [1]: chunker = pd.read_csv('DATASET.csv', chunksize=500, header=0)

# Store the dtypes of each chunk into a list and convert it to a dataframe:

In [2]: dtypes = pd.DataFrame([chunk.dtypes for chunk in chunker])

In [3]: dtypes.values[:5]
Out[3]:
array([[int64, int64, int64, object, int64, int64, int64, int64],
       [int64, int64, int64, int64, int64, int64, int64, int64],
       [int64, int64, int64, int64, int64, int64, int64, int64],
       [int64, int64, int64, int64, int64, int64, int64, int64],
       [int64, int64, int64, int64, int64, int64, int64, int64]], dtype=object)

# Very cool that I can take the max of these data types and it will preserve the hierarchy:

In [4]: dtypes.max().values
Out[4]: array([int64, int64, int64, object, int64, int64, int64, int64], dtype=object)

# I can now store the above into a dictionary:

types = dtypes.max().to_dict()

# And pass it into pd.read_csv fo the second run:

chunker = pd.read_csv('tree_prop_dset.csv', dtype=types, chunksize=500)

您还可以使用skiprows=要跳过的行列表,并让它跳过1-9中的每一行,因此每10行采样一次会快得多(并且可能会得到您想要的答案),我认为您需要生成跳过的行列表yourself@Jeff在不知道csv中的行总数的情况下,您如何获得此信息,在所有的块迭代之后,还应该考虑选择max dType,而不是存储所有dType并在最后进行还原。例如,一个有2毫米行、块大小为500的csv将在帧中产生400000行!