I am trying to write a dataframe to excel using panda.ExcelWriter
after reading it from a huge csv file.
This code updates the excel sheet but it doesn't appends the data to the excel which I want
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()
I also tried to use data.append()
and data.to_excel
doing this result is memory error. Since I am reading data in chunks is there any way to write the data to excel
I got it working by this code
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()
But still take so much time
我不知道这是否是导致主要问题的原因,但是您不应该在块之间调用save()
,因为单次调用save()
关闭xlsxwriter
文件。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.