2014-09-20 28 views
1

我想從一個巨大的csv文件中讀取數據框後,使用panda.ExcelWriter寫入excel。添加數據框到excel表

此代碼更新Excel工作表,但它不會將數據追加到Excel中,我想

import pandas as pd 

reader = pd.read_csv("H:/ram/temp/1.csv", delimiter = '\t' ,chunksize = 10000, names = ['neo_user_id', 
    'gender', 
    'age_range', 
    'main_geolocation', # (user identifier of the client) 
    'interest_category_1', 
    'interest_category_2', 
    'interest_category_3', 
    'first_day_identifier' 
    ], encoding="utf-8") 

ew = pd.ExcelWriter('H:/ram/Formatted/SynthExport.xlsx', engine='xlsxwriter', options={'encoding':'utf-8'}) 
for chunks in reader: 
    chunks.to_excel(ew, 'Sheet1' , encoding = 'utf-8') 
    print len(chunks) 
ew.save() 

我還試圖用data.append()data.to_excel這樣做的結果是內存錯誤。因爲我在塊讀取數據,有沒有辦法將數據寫入到Excel

我知道了這個代碼工作

import pandas as pd 
import xlsxwriter 
reader = pd.read_csv("H:/ram/user_action_export.2014.01.csv", delimiter = '\t', chunksize = 1000, names = ['day_identifier', 
    'user_id', 
    'site_id', 
    'device', # (user identifier of the client) 
    'geolocation', 
    'referrer', 
    'pageviews', 
    ], encoding="utf-8") 

startrows = 0 
ew = pd.ExcelWriter('H:/ram/Formatted/ActionExport.xlsx', engine='xlsxwriter', options={'encoding':'utf-8'}) 

for chunks in reader: 
    chunks.to_excel(ew, 'Sheet1' , encoding = 'utf-8', startrow = startrows) 
    startrows = startrows + len(chunks) 
    print startrows 

ew.save() 

但仍然需要這麼多時間

+1

excel作者採取開始行參數。您可以跟蹤塊的長度並使用它來查找下一個空行。 – b10n 2014-09-20 19:54:09

回答

0

我不知道如果它導致主要問題,但不應該在塊之間調用save(),因爲一次調用save()會關閉xlsxwriter文件。

+0

該csv中有9182321行,它需要8270.3s才能完成,有沒有更快的方法來做到這一點,因爲excel中的最大行數是1048576,整個數據都沒有被複制。在哪裏保存? – user2963604 2014-09-20 23:14:57

+0

900萬行x N列是大量數據,而XLSX不是簡單的文件格式。在中型機器上,使用XlsxWriter可以達到每百萬單元20秒。使用XlsxWriter中的[constant_memory](https://xlsxwriter.readthedocs.org/working_with_memory.html)模式,它可能會更快一些(每百萬個單元15秒),並且不會消耗系統中的所有內存。但是,您需要在熊貓之外使用它。 – jmcnamara 2014-09-21 09:41:49

+0

我的系統配置是4 GB的RAM 640 GB的硬盤i5處理器Widows8 1.7 Ghz和我想寫一個Excel文件存在於外部硬盤 – user2963604 2014-09-21 14:48:51