Created
October 17, 2018 15:23
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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