How to vstack multiple astropy tables using a loop

Viewed 55

I have a list of astropy tables and I want to vstack them all using a loop.

This is what I have:

tables = []

for i in range(len(sortedfiles)): tables.append(openfits(sortedfiles[i]))

which creates a list where each element is an astropy table. I have a total of 144 such tables.

I want to vertically stack them so I have effectively just the one astropy table but I cannot think of the right kind of loop. Any ideas? TIA

1 Answers

You are very close! All that is left is:

from astropy.table import vstack
table_stacked = vstack(tables)

What you have done to read all the tables up front to a list of Table followed by a single vstack operation at the end is the right way. Do not use vstack within the loop since this will be slower.

Related