Reputation: 1792
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
Reputation: 118
df.groupby
function to split df in a chunk of 500. And save each chunk. You can see the reference here (https://stackoverflow.com/a/25703030/6996326)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