potent_pwnables

csv_chunks.py

Jan 3rd, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. import pandas as pd
  2. from sys import argv
  3.  
  4. def main(INFILE, CHUNK_SIZE):
  5.     df = pd.read_csv(INFILE)
  6.    
  7.     # Assign "groups" to each row
  8.     def assign_chunk(x):
  9.         x += 1
  10.         mod = x % (-1 * CHUNK_SIZE)
  11.         diff = x - mod
  12.         scaled = diff / CHUNK_SIZE
  13.         return int(scaled)
  14.        
  15.     chunks = [assign_chunk(x) for x in df.index.values]
  16.     df['chunk'] = pd.Series(chunks)
  17.     n = chunks[-1] # This gets the last value, which will be the max
  18.    
  19.     for i in range(1, n + 1):
  20.         tmp = df.loc[df['chunk'] == i].copy()
  21.         OUTFILE = INFILE.replace('.csv', ' Chunk {}.csv'.format(i))
  22.         tmp.to_csv(OUTFILE)
  23.        
  24. if __name__ == '__main__':
  25.     INFILE = argv[1]
  26.     if len(argv) > 2:
  27.         CHUNK_SIZE = int(argv[2])
  28.     else:
  29.         CHUNK_SIZE = 100
  30.    
  31.     main(INFILE, CHUNK_SIZE)
Advertisement
Add Comment
Please, Sign In to add comment