If you want to keep working with .csv
, you can read the files in chunks, select and concatenate the pertinent rows from each chunk along the below lines (see docs):
patient_id = id
patient = pd.DataFrame()
for chunk in pd.read_csv(filename, chunksize=chunksize):
patient = pd.concat([patient, chunk[chunk.patient_id==id])
However, I would recommend taking a look at HDF5 storage via pandas
as this allows you to select via queries on indexed data rather than iterating through a file. And there are of course various sql
-based options (see basic example)