Advertisement
CptAwe

Split CSVs

Mar 29th, 2019
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3. import os
  4.  
  5. def splitCSVs(filename='',directory=os.getcwd(), num_of_files=3):
  6.  
  7.     if filename=='': raise ValueError("No filename entered")
  8.  
  9.     os.chdir(directory)
  10.     df=pd.read_csv(filename)
  11.  
  12.     type_of_file=filename[len(filename)-filename[::-1].index('.')-1:]
  13.  
  14.     filename=filename[:filename.index(type_of_file)]
  15.  
  16.  
  17.     print("Original length: %s"%(len(df)))
  18.  
  19.     incr=int(len(df)/num_of_files)
  20.     parts=[]
  21.     for i in range(num_of_files):
  22.         parts.append((i+1)*incr)
  23.     parts=parts[:-1]
  24.  
  25.  
  26.     dfs=np.split(df,parts,axis=0)
  27.  
  28.     num=0
  29.     for df_part in dfs:
  30.         print('  length of part %s: %s'%(num,len(df_part)))
  31.         df_part.to_csv(filename+'_part%s'%(num)+type_of_file, index=False)
  32.         num+=1
  33.  
  34.  
  35. filename='Digital Upload - Shinjuku Minamiguchi_20190312_part2_part1.csv'
  36. directory='/home/cptawe/Desktop/datawise/Temp_Files/'
  37. num_of_files=4
  38.  
  39. splitCSVs(filename,directory,4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement