pandas.DataFrame.to_pickle#
- DataFrame.to_pickle(path, compression='infer', protocol=5, storage_options=None)[源代码]#
Pickle(序列化)对象到文件。
- 参数
- path字符串、路径对象或类似文件的对象
字符串、路径对象(实现
os.PathLike[str]
)或实现二进制文件的类似文件的对象write()
功能。将存储已腌渍对象的文件路径。- compression字符串或词典,默认为‘INFER’
For on-the-fly compression of the output data. If 'infer' and 'path' path-like, then detect compression from the following extensions: '.gz', '.bz2', '.zip', '.xz', or '.zst' (otherwise no compression). Set to
None
for no compression. Can also be a dict with key'method'
set to one of {'zip'
,'gzip'
,'bz2'
,'zstd'
} and other key-value pairs are forwarded tozipfile.ZipFile
,gzip.GzipFile
,bz2.BZ2File
, orzstandard.ZstdDecompressor
, respectively. As an example, the following could be passed for faster compression and to create a reproducible gzip archive:compression={'method': 'gzip', 'compresslevel': 1, 'mtime': 1}
.- protocol集成
Int,指示Pickler应使用哪种协议,默认最高协议(请参见 [1] 第12.1.2段)。可能的值为0、1、2、3、4、5。PROTOCOL参数的负值相当于将其值设置为HOSTEST_PROTOCOL。
- storage_optionsDICT,可选
对特定存储连接有意义的额外选项,例如主机、端口、用户名、密码等。对于HTTP(S)URL,键-值对被转发到
urllib.request.Request
作为标题选项。对于其他URL(例如,以“s3://”和“gcs://”开头),键-值对被转发到fsspec.open
。请看fsspec
和urllib
有关更多详细信息和有关存储选项的更多示例,请参阅 here 。1.2.0 新版功能.
参见
read_pickle
从文件中加载腌制Pandas对象(或任何对象)。
DataFrame.to_hdf
将DataFrame写入HDF5文件。
DataFrame.to_sql
将DataFrame写入SQL数据库。
DataFrame.to_parquet
将DataFrame写入二进制拼图格式。
示例
>>> original_df = pd.DataFrame({"foo": range(5), "bar": range(5, 10)}) >>> original_df foo bar 0 0 5 1 1 6 2 2 7 3 3 8 4 4 9 >>> original_df.to_pickle("./dummy.pkl")
>>> unpickled_df = pd.read_pickle("./dummy.pkl") >>> unpickled_df foo bar 0 0 5 1 1 6 2 2 7 3 3 8 4 4 9