Using constant memory with pandas xlsxwriter

Viewed 1321

Im trying to use the below code to write large pandas dataframes to excel worsheets, if i write it directly the system is running out of RAM, is this a viable option or are there any alternatives?

writer = pd.ExcelWriter('Python Output Analysis.xlsx', engine='xlsxwriter',options=dict(constant_memory=True))
2 Answers

The constant_memory mode of XlsxWriter can be used to write very large Excel files with low, constant, memory usage. The catch is that the data needs to be written in row order and (as @Stef points out in the comments above) Pandas writes to Excel in column order. So constant_memory mode won't work with Pandas ExcelWriter.

As an alternative you could avoid ExcelWriter and write the data directly to XlsxWriter from the dataframe on a row by row basis. However, that will be slower from a Pandas point of view.

If your data is large, just consider saving the data with raw text file. e.g. csv, txt, etc.

df.to_csv('file.csv', index=False, sep=',')
df.to_csv('file.tsv', index=False, sep='\t')

Or split DataFrame, and save to small files.

df_size = df.shape[0]
chunksize = df_size//10
for i in range(0, df_size, chunksize):
    # print(i, i+chunksize)
    dfn = df.iloc[i:i+chunksize,:]
    dfn.to_excel('...')  
Related