Skip to content

Instantly share code, notes, and snippets.

@f-huang
Created June 25, 2019 08:35
Show Gist options
  • Save f-huang/d2a949ecc37ec714e198c45498c0b779 to your computer and use it in GitHub Desktop.
Save f-huang/d2a949ecc37ec714e198c45498c0b779 to your computer and use it in GitHub Desktop.
Python function to show progress bar while reading a CSV.
import math
import pandas as pd
def read_csv_in_chunks(path, n_lines, **read_params):
if 'chunksize' not in read_params or read_params['chunksize'] < 1:
read_params['chunksize'] = 80000
chunks = [0] * math.ceil(n_lines / read_params['chunksize'])
for i, chunk in enumerate(pd.read_csv(path, **read_params)):
percent = min(((i + 1) * read_params['chunksize'] / n_lines) * 100, 100.0)
print("#" * int(percent), f"{percent:.2f}%", end='\r', flush=True)
chunks[i] = chunk
df = pd.concat(chunks, axis=0)
del chunks
print()
return df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment