Reputation: 2185
I have an astropy table I'm reading from a file. For the sake of building a working example, let's make it
from astropy.table import Table, Column
t = Table(names=('a', 'b', 'c'))
t.add_row((1, 2.0, 4.2))
t.add_row((2, 5.0, 1.7))
t
I'm making another table with the same columns as t
.
s = Table(names = t.colnames)
And I would like to add certain lines from t
into s
row = t[t['a'] == 1]
s.add_row(row)
raises
ValueError: Mismatch between number of vals and columns
Why am I getting this error, and how can I add row
to s
?
Upvotes: 0
Views: 450
Reputation: 2552
The easiest way to make a new table with the same columns as t
is with
s = Table(t[0:0])
This is a slight trick that relies on making a zero-length version of t
.
One problem with your code s = Table(names=t.colnames)
is that without giving any information about the data types via the dtype
parameter, this initializer gives all float64
columns.
Then to add certain rows to s
you would do:
for row in t[t['a'] == 1]: # Iterate over temporary table that is a subset of t
s.add_row(row)
In your original version, row
is a Table
not a Row
and so using add_row()
with a Table
input does not work. The error message you got is definitely not helpful and I'll open an issue to make that more informative in this case.
It is important to note that in general making a table one row at a time will be slow, so if they tables are large then you should try to find another way. In this case you could do s = t[t['a'] == 1]
, or explore options using vstack (https://docs.astropy.org/en/stable/api/astropy.table.vstack.html).
Upvotes: 2