splitting one csv into multiple files in python

前端 未结 10 2132
执念已碎
执念已碎 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 08:03

    A simpler script works for me.

    import pandas as pd
    path = "path to file" # path to file
    df = pd.read_csv(path) # reading file
    
    low = 0 # Initial Lower Limit
    high = 1000 # Initial Higher Limit
    while(high < len(df)):
        df_new = df[low:high] # subsetting DataFrame based on index
        low = high #changing lower limit
        high = high + 1000 # givig uper limit with increment of 1000
        df_new.to_csv("Path to output file") # output file 
    

提交回复
热议问题