Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- from sys import argv
- def main(INFILE, CHUNK_SIZE):
- df = pd.read_csv(INFILE)
- # Assign "groups" to each row
- def assign_chunk(x):
- x += 1
- mod = x % (-1 * CHUNK_SIZE)
- diff = x - mod
- scaled = diff / CHUNK_SIZE
- return int(scaled)
- chunks = [assign_chunk(x) for x in df.index.values]
- df['chunk'] = pd.Series(chunks)
- n = chunks[-1] # This gets the last value, which will be the max
- for i in range(1, n + 1):
- tmp = df.loc[df['chunk'] == i].copy()
- OUTFILE = INFILE.replace('.csv', ' Chunk {}.csv'.format(i))
- tmp.to_csv(OUTFILE)
- if __name__ == '__main__':
- INFILE = argv[1]
- if len(argv) > 2:
- CHUNK_SIZE = int(argv[2])
- else:
- CHUNK_SIZE = 100
- main(INFILE, CHUNK_SIZE)
Advertisement
Add Comment
Please, Sign In to add comment