SOK
SOK

Reputation: 1792

How to export a dataframe to csv in chunks of 500 rows

I have a large dataframe which i export to a CSV and upload to a third party product. The third party product can only accept uploads of max 500 rows of my data so I am wondering how to export a dataframe into smaller files.

at the moment my code reads:

df.to_csv("Export.csv",index=False)

But ideally would like code to export files so that it gives:

Export1.csv
Export2.csv
Export3.csv

etc until it is all completed and each with 500 rows (and then whatever is left over). Any help would be much appreciated!

Upvotes: 1

Views: 3704

Answers (1)

vraj
vraj

Reputation: 118

import numpy as np
no_of_rows = 500
for k,g in df.groupby(np.arange(len(df))//no_of_rows):
    g.to_csv('Export{}.csv'.format(k+1), index=False)

Upvotes: 5

Related Questions