Pandas dataframe to_csv - split into multiple output files
Asked Answered
S

2

19

What is the best /easiest way to split a very large data frame (50GB) into multiple outputs (horizontally)?

I thought about doing something like:

stepsize = int(1e8)
for id, i in enumerate(range(0,df.size,stepsize)): 
    start = i 
    end = i + stepsize-1 #neglect last row ...
    df.ix[start:end].to_csv('/data/bs_'+str(id)+'.csv.out')

But I bet there is a smarter solution out there?

As noted by jakevdp, HDF5 is a better way to store huge amounts of numerical data, however it doesn't meet my business requirements.

Sidedress answered 12/6, 2017 at 14:45 Comment(0)
S
25

This answer brought me to a satisfying solution using:

for idx, chunk in enumerate(np.array_split(df, number_of_chunks)):
    chunk.to_csv(f'/data/bs_{idx}.csv')
Sidedress answered 12/6, 2017 at 15:11 Comment(1)
You have attached the wrong link, it should be, numpy.org/doc/stable/reference/generated/numpy.array_split.htmlTypical
K
17

Use id in the filename else it will not work. You missed id, and without id, it gives an error.

for id, df_i in  enumerate(np.array_split(df, number_of_chunks)):
    df_i.to_csv('/data/bs_{id}.csv'.format(id=id))
Kiki answered 3/2, 2019 at 22:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.