Skip to content
Related Articles

Related Articles

Read a zipped file as a Pandas DataFrame
  • Last Updated : 11 Dec, 2020

In this article, we will try to find out how can we read data from a zip file using a panda data frame. 

Why we need a zip file?

People use related groups of files together and to make files compact, so they are easier and faster to share via the web. Zip files are ideal for archiving since they save storage space. And, they are also useful for securing data using the encryption method.

Requirement: 

zipfile36 module: This module is used to perform various operations on a zip file using a simple python program. It can be installed using the below command:

pip install zipfile36

Method #1: Using compression=zip in pandas.read_csv() method. 



By assigning the compression argument in read_csv() method as zip, then pandas will first decompress the zip and then will create the dataframe from CSV file present in the zipped file. 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import required modules
import zipfile
import pandas as pd
  
# read the dataset using the compression zip
df = pd.read_csv('test.zip',compression='zip')
  
# display dataset
print(df.head())

chevron_right


Output:

Method #2: Opening the zip file to get the CSV file.

Here, initially, the zipped file is opened and the CSV file is extracted, and then a dataframe is created from the extracted CSV file. 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# import required modules
import zipfile
import pandas as pd
  
# oppen zipped dataset
with zipfile.ZipFile("test.zip") as z:
   # open the csv file in the dataset
   with z.open("test.csv") as f:
        
      # read the dataset
      train = pd.read_csv(f)
        
      # display dataset
      print(train.head())

chevron_right


Output:


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :