How to use the gzip module to open a csv file

Viewed 228

I am looking to read in a .csv.gz file that is in the same directory as my python script using the gzip and pandas module only.

So far I have,

import gzip
import pandas as pd
data = gzip.open(test_data.csv.gz, mode='rb')

How do I proceed in converting / reading this file in as a dataframe without using the csv module as seen in similarly answered questions?

1 Answers

You can use pandas.read_csv directly:

import pandas as pd
df = pd.read_csv('test_data.csv.gz', compression='gzip')

If you must use gzip:

with gzip.open('test_data.csv.gz', mode='rb') as csv:
    df = pd.read_csv(csv)
Related