I have several (tens to hundreds) DataFrames that I'm reading from .tab/.csv files. The DFs are substantial (thousands of rows, hundreds of columns, tens of MBs), but I only need to pick out just a couple of values from each DataFrame.
This is straightforward enough to do by looping through the files and extracting the needed values from each:
values = []
for file in datalist:
df = pd.read_csv(file)
# Identify values by row/column labels as positions may change
val0 = df.loc['row_index_0', 'column_number_0']
val1 = df.loc['row_index_1', 'column_number_1']
val2 = df.loc['row_index_2', 'column_number_2']
values.append([val0, val1, val2])
But I'm wondering if I need to worry about doing it efficiently, and if so, how best to do that. Like, do I need to worry about a simple for loop like that creating a bunch of sizeable df objects that then float around taking up memory?
And if that's a potential problem, what can I do to make sure the df objects are purged once no longer needed? Is there a simple context manager approach like using with statements?