The Wayback Machine - https://web.archive.org/web/20240613120945/https://www.geeksforgeeks.org/read-a-zipped-file-as-a-pandas-dataframe/
Open In App

Read a zipped file as a Pandas DataFrame

Last Updated : 28 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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())


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




# import required modules
import zipfile
import pandas as pd
 
# open 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())


Output:



Similar Reads

Python | Ways to sort a zipped list by values
Zipped lists are those lists where several lists are mapped together to form one list which can be used as one entity altogether. In Python Zip() function is used to map different lists. Let's discuss a few methods to demonstrate the problem. Method #1: Using lambda and sort C/C++ Code # Python code to demonstrate # sort zipped list by values # usi
2 min read
How to read a CSV file to a Dataframe with custom delimiter in Pandas?
Python is a good language for doing data analysis because of the amazing ecosystem of data-centric python packages. pandas package is one of them and makes importing and analyzing data so much easier.Here, we will discuss how to load a csv file into a Dataframe. It is done using a pandas.read_csv() method. We have to import pandas library to use th
3 min read
Read SQL database table into a Pandas DataFrame using SQLAlchemy
To read sql table into a DataFrame using only the table name, without executing any query we use read_sql_table() method in Pandas. This function does not support DBAPI connections. read_sql_table()Syntax : pandas.read_sql_table(table_name, con, schema=None, index_col=None, coerce_float=True, parse_dates=None, columns=None, chunksize=None) Paramete
2 min read
How to read all excel files under a directory as a Pandas DataFrame ?
In this article, we will see how to read all Excel files in a folder into single Pandas dataframe. The task can be performed by first finding all excel files in a particular folder using glob() method and then reading the file by using pandas.read_excel() method and then displaying the content. Approach:Import necessary python packages like pandas,
1 min read
Read Text file into PySpark Dataframe
In this article, we are going to see how to read text files in PySpark Dataframe. There are three ways to read text files into PySpark DataFrame. Using spark.read.text()Using spark.read.csv()Using spark.read.format().load() Using these we can read a single text file, multiple files, and all files from a directory into Spark DataFrame and Dataset. T
3 min read
PySpark - Read CSV file into DataFrame
In this article, we are going to see how to read CSV files into Dataframe. For this, we will use Pyspark and Python. Files Used: authorsbook_authorbooksRead CSV File into DataFrame Here we are going to read a single CSV into dataframe using spark.read.csv and then create dataframe with this data using .toPandas(). C/C++ Code from pyspark.sql import
2 min read
How to read csv file with Pandas without header?
Prerequisites: Pandas A header of the CSV file is an array of values assigned to each of the columns. It acts as a row header for the data. This article discusses how we can read a csv file without header using pandas. To do this header attribute should be set to None while reading the file. Syntax: read_csv("file name", header=None) ApproachImport
1 min read
Read Html File In Python Using Pandas
In Python, Pandas is a powerful library commonly used for data manipulation and analysis. While it's primarily used for working with structured data such as CSV files, Excel spreadsheets, and databases, it's also capable of reading HTML files and extracting tabular data from them. In this article, we'll explore how to read an HTML file in Python us
5 min read
Difference Between Spark DataFrame and Pandas DataFrame
Dataframe represents a table of data with rows and columns, Dataframe concepts never change in any Programming language, however, Spark Dataframe and Pandas Dataframe are quite different. In this article, we are going to see the difference between Spark dataframe and Pandas Dataframe. Pandas DataFrame Pandas is an open-source Python library based o
3 min read
Pandas Dataframe.to_numpy() - Convert dataframe to Numpy array
Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). This data structure can be converted to NumPy ndarray with the help of the DataFrame.to_numpy() method. In this article we will see how to convert dataframe to numpy array. Syntax of Pandas DataFrame.to_numpy()
3 min read
Practice Tags :