Skip to content

Instantly share code, notes, and snippets.

@rikturr
Created October 17, 2018 15:23
Show Gist options
  • Save rikturr/888c402e788e560e38380a7f6ac352d7 to your computer and use it in GitHub Desktop.
Save rikturr/888c402e788e560e38380a7f6ac352d7 to your computer and use it in GitHub Desktop.
Convert a large CSV file with binary class label to HDF5 file. Helpful if CSV is too big to fit in memory, HDF5 allows for indexing straight from file
import pandas as pd
from datetime import datetime
CHUNK_SIZE = 1000000
POS_KEY = 'positive'
NEG_KEY = 'negative'
CLASS_COLUMN = 'class'
FILE = '<FILEPATH>'
OUTFILE = '<OUTPATH>'
store = pd.HDFStore(OUTFILE, complib='blosc', complevel=9)
i = 0
for chunk in pd.read_csv(FILE, chunksize=CHUNK_SIZE):
print('{} {}'.format(i, datetime.now()))
store.append(POS_KEY, chunk[chunk[CLASS_COLUMN] == 1], index=False)
store.append(NEG_KEY, chunk[chunk[CLASS_COLUMN] == 0], index=False)
i += 1
store.create_table_index(POS_KEY, columns=True, optlevel=9, kind='full')
store.create_table_index(NEG_KEY, columns=True, optlevel=9, kind='full')
store.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment