Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import numpy as np
- import os
- def splitCSVs(filename='',directory=os.getcwd(), num_of_files=3):
- if filename=='': raise ValueError("No filename entered")
- os.chdir(directory)
- df=pd.read_csv(filename)
- type_of_file=filename[len(filename)-filename[::-1].index('.')-1:]
- filename=filename[:filename.index(type_of_file)]
- print("Original length: %s"%(len(df)))
- incr=int(len(df)/num_of_files)
- parts=[]
- for i in range(num_of_files):
- parts.append((i+1)*incr)
- parts=parts[:-1]
- dfs=np.split(df,parts,axis=0)
- num=0
- for df_part in dfs:
- print(' length of part %s: %s'%(num,len(df_part)))
- df_part.to_csv(filename+'_part%s'%(num)+type_of_file, index=False)
- num+=1
- filename='Digital Upload - Shinjuku Minamiguchi_20190312_part2_part1.csv'
- directory='/home/cptawe/Desktop/datawise/Temp_Files/'
- num_of_files=4
- splitCSVs(filename,directory,4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement