用 Python 编写一个程序,将给定的数据框导出为 Pickle 文件格式并从 Pickle 文件中读取内容
pythonpandasserver side programmingprogramming更新于 2024/6/21 5:00:17
假设您有一个数据框和要导出到 pickle 文件的结果,并从文件中读取内容,
Export to pickle file: Read contents from pickle file: Fruits City 0 Apple Shimla 1 Orange Sydney 2 Mango Lucknow 3 Kiwi Wellington
解决方案
为了解决这个问题,我们将遵循下面给出的步骤 −
定义一个数据框。
将数据框导出为 pickle 格式并将其命名为 ‘pandas.pickle’,
df.to_pickle('pandas.pickle')
从 ‘pandas.pickle’ 读取内容文件并将其存储为结果,
result = pd.read_pickle('pandas.pickle')
示例
让我们看看下面的实现以便更好地理解,
import pandas as pd
df = pd.DataFrame({'Fruits': ["Apple","Orange","Mango","Kiwi"],
'City' : ["Shimla","Sydney","Lucknow","Wellington"]
})
print("Export to pickle file:")
df.to_pickle('pandas.pickle')
print("Read contents from pickle file:")
result = pd.read_pickle('pandas.pickle')
print(result)
输出
Export to pickle file: Read contents from pickle file: Fruits City 0 Apple Shimla 1 Orange Sydney 2 Mango Lucknow 3 Kiwi Wellington
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

