Read a zipped file as a pandas DataFrame

后端 未结 5 1677
暖寄归人
暖寄归人 2020-12-07 11:52

I\'m trying to unzip a csv file and pass it into pandas so I can work on the file.
The code I have tried so far is:

import requests, zipfile, StringIO
r         


        
5条回答
  •  无人及你
    2020-12-07 12:01

    If you want to read a zipped or a tar.gz file into pandas dataframe, the read_csv methods includes this particular implementation.

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

    Or the long form:

    df = pd.read_csv('filename.zip', compression='zip', header=0, sep=',', quotechar='"')
    

    Description of the compression argument from the docs:

    compression : {‘infer’, ‘gzip’, ‘bz2’, ‘zip’, ‘xz’, None}, default ‘infer’ For on-the-fly decompression of on-disk data. If ‘infer’ and filepath_or_buffer is path-like, then detect compression from the following extensions: ‘.gz’, ‘.bz2’, ‘.zip’, or ‘.xz’ (otherwise no decompression). If using ‘zip’, the ZIP file must contain only one data file to be read in. Set to None for no decompression.

    New in version 0.18.1: support for ‘zip’ and ‘xz’ compression.

提交回复
热议问题