如何将Pandas Dataframe保存为gzip/zip文件

如何将Pandas Dataframe保存为gzip/zip文件

Pandas是一个建立在NumPy库之上的开源库。它是一个Python包,提供各种数据结构和操作,用于处理数字数据和时间序列。它主要因导入和分析数据更容易而流行。Pandas速度很快,它对用户来说具有高性能和生产力。

转换为zip/gzip文件

Pandas中的to_pickle()方法是用来将给定的对象腌制(序列化)到文件中。这个方法利用了下面的语法。

语法:

DataFrame.to_pickle(self, path,
                   compression='infer',
                   protocol=4)
Python

这个方法支持zip、gzip、bz2和xz等压缩方式。在给定的例子中,你会看到如何将一个DataFrame转换成zip和gzip。

例子1:将Pandas数据框架保存为zip文件

# importing packages
import pandas as pd
  
# dictionary of data
dct = {'ID': {0: 23, 1: 43, 2: 12,
  
              3: 13, 4: 67},
  
       'Name': {0: 'Ajay', 1: 'Deep',
  
                2: 'Deepanshi', 3: 'Mira',
  
                4: 'Yash'},
  
       'Marks': {0: 89, 1: 97, 2: 45, 3: 78,
  
                 4: 56},
  
       'Grade': {0: 'B', 1: 'A', 2: 'F', 3: 'C',
  
                 4: 'E'}
       }
  
# forming dataframe and printing
data = pd.DataFrame(dct)
print(data)
  
# using to_pickle function to form file
# by default, compression type infers from the file extension in specified path.
# file will be created in the given path
data.to_pickle('file.zip')
Python

输出:

如何将Pandas Dataframe保存为gzip/zip文件?

示例2:将Pandas数据框架保存为gzip文件。

# importing packages
import pandas as pd
  
# dictionary of data
dct = {"C1": range(5), "C2": range(5, 10)}
  
# forming dataframe and printing
data = pd.DataFrame(dct)
print(data)
  
# using to_pickle function to form file
# we can also select compression type
# file will be created in the given path
data.to_pickle('file.gzip')
Python

输出:

如何将Pandas Dataframe保存为gzip/zip文件?

读取zip/gzip文件

为了读取创建的文件,你需要使用read_pickle()方法。这个方法利用了下面的语法。

pandas.read_pickle(filepath_or_buffer,  
               compression='infer')
Python

示例1:读取压缩文件

# reading from the zip file
pd.read_pickle('file.zip')
Python

输出:

如何将Pandas Dataframe保存为gzip/zip文件?

示例2:读取gzip文件。

# reading from gzip file
pd.read_pickle('file.gzip')
Python

输出:

如何将Pandas Dataframe保存为gzip/zip文件?

从上面两个例子中,我们可以看到这两个压缩文件都可以被read_pickle()方法读取,除了文件扩展名之外没有任何变化。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册