Skip to content Skip to sidebar Skip to footer

Pandas: Convert Winzipped Csv File To Data Frame

I have a couple of WinZipped csv files and would like to read these in as a Pandas dataframe. The problem is that neither of the decompression options ('gzip' or 'bz2') seems to wo

Solution 1:

You just need to unzip the file:

with zipfile.ZipFile('/path/to/file', 'r') as z:
    f = z.open('member.csv')
    table = pd.io.parsers.read_table(f, ...)

The filepath_or_buffer parameter to read_table accepts any file-like argument.

Solution 2:

Pandas 0.18.1 added Zip support out of the box:

df = pd.read_csv('my_zipped_csv.zip', compression = 'zip')

In fact, since the default param is compression = 'infer', you can just throw anything with a .zip extension at it, and it'll know what to do:

df = pd.read_csv('my_zipped_csv.zip')

Post a Comment for "Pandas: Convert Winzipped Csv File To Data Frame"