splitting one csv into multiple files in python

前端 未结 10 2131
执念已碎
执念已碎 2020-12-05 07:28

I have a csv file of about 5000 rows in python i want to split it into five files.

I wrote a code for it but it is not working

import codecs
import c         


        
10条回答
  •  臣服心动
    2020-12-05 07:48

    Another pandas solution (each 1000 rows), similar to Aziz Alto solution:

    suffix = 1
    for i in range(len(df)):
        if i % 1000 == 0:
            df[i:i+1000].to_csv(f"processed/{filename}_{suffix}.csv", sep ='|', index=False, index_label=False)
            suffix += 1
    

    where df is the csv loaded as pandas.DataFrame; filename is the original filename, the pipe is a separator; index and index_label false is to skip the autoincremented index columns

提交回复
热议问题