现在,每次运行脚本时,我都会导入一个相当大的CSV作为数据框架。是否有一个好的解决方案来保持数据帧在运行之间不断可用,这样我就不必花费所有的时间等待脚本运行?
当前回答
如果我理解正确的话,你已经在使用pandas.read_csv(),但想要加快开发过程,这样你就不必每次编辑脚本时都加载文件,对吗?我有一些建议:
you could load in only part of the CSV file using pandas.read_csv(..., nrows=1000) to only load the top bit of the table, while you're doing the development use ipython for an interactive session, such that you keep the pandas table in memory as you edit and reload your script. convert the csv to an HDF5 table updated use DataFrame.to_feather() and pd.read_feather() to store data in the R-compatible feather binary format that is super fast (in my hands, slightly faster than pandas.to_pickle() on numeric data and much faster on string data).
您可能还会对stackoverflow上的答案感兴趣。
其他回答
泡菜很好!
import pandas as pd
df.to_pickle('123.pkl') #to save the dataframe, df to 123.pkl
df1 = pd.read_pickle('123.pkl') #to load 123.pkl back to the dataframe df
虽然已经有一些答案,我找到了一个很好的比较,他们尝试了几种方法来序列化熊猫数据框架:有效地存储熊猫数据框架。
他们比较:
pickle:原始ASCII数据格式 cPickle,一个C库 Pickle-p2:使用更新的二进制格式 Json: standardlib Json库 json-no-index:类似json,但没有索引 msgpack:二进制JSON替代品 CSV hdfstore: HDF5存储格式
在他们的实验中,他们序列化了一个包含1,000,000行的DataFrame,并分别测试了两列:一列是文本数据,另一列是数字。他们的免责声明说:
您不应该相信以下内容适用于您的数据。您应该查看自己的数据并自己运行基准测试
他们提到的测试源代码可以在网上找到。由于这段代码不能直接工作,我做了一些小更改,您可以在这里看到:serialize.py 我得到了以下结果:
他们还提到,通过将文本数据转换为分类数据,序列化速度要快得多。在他们的测试中,大约是10倍的速度(另见测试代码)。
编辑:pickle的时间比CSV的时间长可以用所使用的数据格式来解释。默认情况下,pickle使用可打印的ASCII表示,这会生成更大的数据集。然而,从图中可以看出,使用更新的二进制数据格式(版本2,pickle-p2)的pickle加载时间要短得多。
其他参考资料:
在“最快的Python库来读取CSV文件”这个问题中,有一个非常详细的答案,它比较了不同的库来读取CSV文件的基准。结果是,对于读取csv文件,numpy.fromfile是最快的。 另一个序列化测试 显示msgpack, usjson和cPickle在序列化中是最快的。
Pandas DataFrame有to_pickle函数,这对于保存DataFrame非常有用:
import pandas as pd
a = pd.DataFrame({'A':[0,1,0,1,0],'B':[True, True, False, False, False]})
print a
# A B
# 0 0 True
# 1 1 True
# 2 0 False
# 3 1 False
# 4 0 False
a.to_pickle('my_file.pkl')
b = pd.read_pickle('my_file.pkl')
print b
# A B
# 0 0 True
# 1 1 True
# 2 0 False
# 3 1 False
# 4 0 False
Numpy文件格式对于数字数据来说非常快
我更喜欢使用numpy文件,因为它们快速且易于使用。 下面是一个简单的基准测试,用于保存和加载一个包含100万个点的1列数据框架。
import numpy as np
import pandas as pd
num_dict = {'voltage': np.random.rand(1000000)}
num_df = pd.DataFrame(num_dict)
使用ipython的%%timeit魔法函数
%%timeit
with open('num.npy', 'wb') as np_file:
np.save(np_file, num_df)
输出为
100 loops, best of 3: 5.97 ms per loop
将数据加载回数据框架
%%timeit
with open('num.npy', 'rb') as np_file:
data = np.load(np_file)
data_df = pd.DataFrame(data)
输出为
100 loops, best of 3: 5.12 ms per loop
不坏!
CONS
如果您使用python 2保存numpy文件,然后尝试使用python 3打开,则会出现问题(反之亦然)。
如果我理解正确的话,你已经在使用pandas.read_csv(),但想要加快开发过程,这样你就不必每次编辑脚本时都加载文件,对吗?我有一些建议:
you could load in only part of the CSV file using pandas.read_csv(..., nrows=1000) to only load the top bit of the table, while you're doing the development use ipython for an interactive session, such that you keep the pandas table in memory as you edit and reload your script. convert the csv to an HDF5 table updated use DataFrame.to_feather() and pd.read_feather() to store data in the R-compatible feather binary format that is super fast (in my hands, slightly faster than pandas.to_pickle() on numeric data and much faster on string data).
您可能还会对stackoverflow上的答案感兴趣。