从多个Zip文件中提取CSV文件并合并数据

3

我有一个Python脚本和pandas用于合并多个ZIP文件。我正在使用托管在这里的GitHub存储库中的数据:https://github.com/statistikat/coronaDAT

该脚本应该获取文件夹结构中所有ZIP文件,在ZIP文件中找到“Bezirke.csv”文件,并将所有Bezirke.csv文件组合成一个大型CSV文件。然而,该代码只从文件夹中抓取一个ZIP文件。

为什么它没有从文件夹中的其他ZIP文件中获取数据,您有任何建议吗?

import glob
from zipfile import ZipFile

path = r'/Users/matt/test/' # use your path

#load all zip files in folder
all_files = glob.glob(path + "/*.zip")

li = []

for filename in all_files:

    zip_file = ZipFile(filename)
    df = {text_file.filename: pd.read_csv(zip_file.open(text_file.filename), delimiter=';', header=0, index_col=['Timestamp'], parse_dates=['Timestamp'])
       for text_file in zip_file.infolist()
       if text_file.filename.endswith('Bezirke.csv')}

    li.append(df)

#print dataframe in console
print(df)

#prepare date to export to csv
frame = pd.concat(df, axis=0)

#export to csv
frame.to_csv( "combined_zip_Bezirke.csv", encoding='utf-8-sig')
print("Export to CSV Successful")```

这句与编程有关的内容应该翻译成中文:难道不应该是frame = pd.concat(li, axis=0)吗?因为df只会包含最终CSV文件读取相关的数据。 - sushanth
啊,是的。我认为你说得对。但是,如果我放入li,那么我会收到以下错误消息:Traceback(最近一次调用的内容): File“merge_zip.py”,第31行,在<module>中 frame = pd.concat(li) File“/ Users / matt / opt / anaconda3 / lib / python3.7 / site-packages / pandas / core / reshape / concat.py”,第281行,在concat中 sort=sort, File“/ Users / matt / opt / anaconda3 / lib / python3.7 / site-packages / pandas / core / reshape / concat.py”,第357行,在__init__中 raise TypeError(msg) TypeError:无法连接类型为'<class' dict '>'的对象;仅Series和DataFrame objs是有效的 - matt
为什么所有的数据框都被附加到列表中,有任何特定的原因吗?相反,我更喜欢使用DataFrame.append - sushanth
1个回答

2

我对您的代码进行了一些更改,并使用三个样本文件进行了测试:

20200523_010000_orig_csv.zip
20200523_020000_orig_csv.zip
20200523_030000_orig_csv.zip

修改后的代码:

df_master = pd.DataFrame()
flag = False

for filename in all_files:    
    zip_file = ZipFile(filename)
    for text_file in zip_file.infolist():
        if text_file.filename.endswith('Bezirke.csv'):
            df = pd.read_csv(zip_file.open(text_file.filename), 
            delimiter=';', 
            header=0, 
            index_col=['Timestamp'], 
            parse_dates=['Timestamp']
            )
    if not flag:
        df_master = df
        flag = True
    else:
        df_master = pd.concat([df_master, df])

print(df_master.info())

输出:

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 279 entries, 2020-05-23 02:00:00 to 2020-05-23 03:00:00
Data columns (total 4 columns):
Bezirk             279 non-null object
Anzahl             279 non-null int64
Anzahl_Inzidenz    279 non-null object
GKZ                279 non-null int64

文件数据现在正在追加。不确定这是否是优化方面最好的解决方案。如适用,请建议改进措施。

Dataframe输出:

                                           Bezirk  Anzahl   Anzahl_Inzidenz  GKZ
Timestamp                                                                       
2020-05-23 02:00:00                     Amstetten     301  259,228000068898  305
2020-05-23 02:00:00                         Baden     157  107,384937381586  306
2020-05-23 02:00:00                       Bludenz     256  401,795523746743  801
2020-05-23 02:00:00                Braunau am Inn     130  124,511531683396  404
2020-05-23 02:00:00                       Bregenz     274  203,894837888721  802
2020-05-23 02:00:00           Bruck an der Leitha     105  102,931085187727  307
2020-05-23 02:00:00            Bruck-Mürzzuschlag      52  52,5337428271236  621
2020-05-23 02:00:00              Deutschlandsberg      49   80,564278785288  603
2020-05-23 02:00:00                      Dornbirn     153  171,830954279489  803
2020-05-23 02:00:00                      Eferding      53  159,850404150078  405
2020-05-23 02:00:00             Eisenstadt(Stadt)      18  122,976019676163  101
2020-05-23 02:00:00           Eisenstadt-Umgebung      23  53,5793323549281  103
2020-05-23 02:00:00                     Feldkirch     213  198,770051978835  804
2020-05-23 02:00:00                   Feldkirchen      15  50,1052209640244  210
2020-05-23 02:00:00                     Freistadt     120  180,123384518395  406
2020-05-23 02:00:00                   Gänserndorf      99  95,4805856142584  308
2020-05-23 02:00:00                         Gmünd      14  38,0714110896582  309
2020-05-23 02:00:00                       Gmunden      73  71,8284775314619  407
2020-05-23 02:00:00                   Graz(Stadt)     466  161,353988490544  601
2020-05-23 02:00:00                 Graz-Umgebung     246  159,471022948269  606
2020-05-23 02:00:00                  Grieskirchen      74  114,336923100694  408
2020-05-23 02:00:00                       Güssing      24  93,0340737295034  104
2020-05-23 02:00:00                       Hallein      69  114,287607248153  502
2020-05-23 02:00:00          Hartberg-Fürstenfeld     318  350,908167994527  622
2020-05-23 02:00:00                      Hermagor       5  27,4363476733977  203
2020-05-23 02:00:00                    Hollabrunn      37  72,7515828384915  310
2020-05-23 02:00:00                          Horn      58   186,55516243165  311
2020-05-23 02:00:00                          Imst     293  487,877980551485  702
2020-05-23 02:00:00                Innsbruck-Land     388  216,375377820408  703
2020-05-23 02:00:00               Innsbruck-Stadt     410  310,347437741276  701
...                                           ...     ...               ...  ...
2020-05-23 03:00:00        Sankt Johann im Pongau     434  538,641976840877  504
2020-05-23 03:00:00            Sankt Pölten(Land)     267  203,748359329691  319
2020-05-23 03:00:00           Sankt Pölten(Stadt)      90  163,505559189012  302
2020-05-23 03:00:00        Sankt Veit an der Glan      52  95,3166529190725  205
2020-05-23 03:00:00                     Schärding      58  101,209276353674  414
2020-05-23 03:00:00                      Scheibbs     103  248,774243412313  320
2020-05-23 03:00:00                        Schwaz     352   419,68213847126  709
2020-05-23 03:00:00           Spittal an der Drau      42  55,1970666701712  206
2020-05-23 03:00:00                  Steyr(Stadt)      62  162,333411881758  402
2020-05-23 03:00:00                    Steyr-Land     181  299,534976086849  415
2020-05-23 03:00:00              Südoststeiermark      62  72,1374800749299  623
2020-05-23 03:00:00                       Tamsweg      22  108,267716535433  505
2020-05-23 03:00:00                         Tulln     175  168,640564319511  321
2020-05-23 03:00:00               Urfahr-Umgebung     281  328,635752295187  416
2020-05-23 03:00:00                  Villach Land      48  74,2252737056968  207
2020-05-23 03:00:00                 Villach Stadt      21  33,7387336728628  202
2020-05-23 03:00:00                   Vöcklabruck     107  78,5303809824371  417
2020-05-23 03:00:00                     Voitsberg     106  207,189069799261  616
2020-05-23 03:00:00                   Völkermarkt      52  124,170208701466  208
2020-05-23 03:00:00        Waidhofen an der Thaya      73  281,983930778739  322
2020-05-23 03:00:00  Waidhofen an der Ybbs(Stadt)      25  222,005150519492  303
2020-05-23 03:00:00                          Weiz     197  218,057846208339  617
2020-05-23 03:00:00                   Wels(Stadt)      58  93,9621235439921  403
2020-05-23 03:00:00                     Wels-Land      79  108,080006566886  418
2020-05-23 03:00:00                   Wien(Stadt)    3048  160,633172963666  900
2020-05-23 03:00:00         Wiener Neustadt(Land)      86  110,269133617982  323
2020-05-23 03:00:00        Wiener Neustadt(Stadt)      56  123,683106212867  304
2020-05-23 03:00:00                     Wolfsberg      43  81,5536926753404  209
2020-05-23 03:00:00                   Zell am See     359  410,463972925385  506
2020-05-23 03:00:00                        Zwettl      73  172,895646819194  325

[279 rows x 4 columns]

最后,您可以将输出写入CSV文件。我不确定您是否想将文件名附加到输出的CSV文件中,因为您当前的代码每次都会使用相同的名称“Bezirke.csv”。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接