我有一个 pandas 数据框,我想将其拆分为几个较小的 100k 行,然后保存到磁盘上,以便我可以读取数据并一一处理。我尝试过使用dill
和hdf
存储,因为 csv 和原始文本似乎需要很多时间。
我正在对具有约 500k 行和五列混合数据的数据子集进行尝试。两个包含字符串,一个整数,一个浮点数,最后一个包含来自 的二元计数sklearn.feature_extraction.text.CountVectorizer
,存储为scipy.sparse.csr.csr_matrix
稀疏矩阵。
这是我遇到问题的最后一列。转储和加载数据没有问题,但是当我尝试实际访问数据时,它是一个 pandas.Series 对象。其次,该系列中的每一行都是一个包含整个数据集的元组。
# Before dumping, the original df has 100k rows.
# Each column has one value except for 'counts' which has 1400.
# Meaning that df['counts'] give me a sparse matrix that is 100k x 1400.
vectorizer = sklearn.feature_extraction.text.CountVectorizer(analyzer='char', ngram_range=(2,2))
counts = vectorizer.fit_transform(df['string_data'])
df['counts'] = counts
df_split = pandas.DataFrame(np.column_stack([df['string1'][0:100000],
df['string2'][0:100000],
df['float'][0:100000],
df['integer'][0:100000],
df['counts'][0:100000]]),
columns=['string1','string2','float','integer','counts'])
dill.dump(df, open(file[i], 'w'))
df = dill.load(file[i])
print(type(df['counts'])
> <class 'pandas.core.series.Series'>
print(np.shape(df['counts'])
> (100000,)
print(np.shape(df['counts'][0])
> (496718, 1400) # 496718 is the number of rows in my complete data set.
print(type(df['counts']))
> <type 'tuple'>
我是否犯了任何明显的错误,或者有没有更好的方法以这种格式存储这些数据,这种格式不是很耗时?它必须可扩展到包含 1 亿行的完整数据。