1

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

ETsiak
  • 11
  • 1

1 Answers1

3

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.

Tom Aldcroft
  • 2,339
  • 1
  • 14
  • 16
  • Thank you!!!!! I kept doing vstack([tables]) and it created a strange thing, never occurred to me to drop the square brackets so I thought I need to loop around the list and use vstack in the loop.... You are the best! It worked by the way :) – ETsiak Mar 31 '22 at 20:48
  • @ETsiak you already have a list so vstack([tables]) would be calling vstack on a list of lists (containing one element) which is probably undefined – Iguananaut Mar 31 '22 at 22:07