-
craftutils.wrap.dragons.Table.add_columns(cols, indexes=
None, names=None, copy=True, rename_duplicate=False) Add a list of new columns the table using
colsdata objects. If a corresponding list ofindexesis supplied then insert column before eachindexposition in the original list of columns, otherwise append columns to the end of the list.The
colsinput can include any data objects which are acceptable as ~astropy.table.Table column objects or can be converted. This includes mixin columns and scalar or length=1 objects which get broadcast to match the table length.From a performance perspective there is little difference between calling this method once or looping over the new columns and calling
add_column()for each column.Parameters¶
- colslist of object
List of data objects for the new columns
- indexeslist of int or None
Insert column before this position or at end (default).
- nameslist of str
Column names
- copybool
Make a copy of the new columns. Default is True.
- rename_duplicatebool
Uniquify new column names if they duplicate the existing ones. Default is False.
See Also¶
astropy.table.hstack, update, replace_column
Examples¶
Create a table with two columns ‘a’ and ‘b’, then create columns ‘c’ and ‘d’ and append them to the end of the table:
>>> t = Table([[1, 2], [0.1, 0.2]], names=('a', 'b')) >>> col_c = Column(name='c', data=['x', 'y']) >>> col_d = Column(name='d', data=['u', 'v']) >>> t.add_columns([col_c, col_d]) >>> print(t) a b c d --- --- --- --- 1 0.1 x u 2 0.2 y vAdd column ‘c’ at position 0 and column ‘d’ at position 1. Note that the columns are inserted before the given position:
>>> t = Table([[1, 2], [0.1, 0.2]], names=('a', 'b')) >>> t.add_columns([['x', 'y'], ['u', 'v']], names=['c', 'd'], ... indexes=[0, 1]) >>> print(t) c a d b --- --- --- --- x 1 u 0.1 y 2 v 0.2Add second column ‘b’ and column ‘c’ with
rename_duplicate:>>> t = Table([[1, 2], [0.1, 0.2]], names=('a', 'b')) >>> t.add_columns([[1.1, 1.2], ['x', 'y']], names=('b', 'c'), ... rename_duplicate=True) >>> print(t) a b b_1 c --- --- --- --- 1 0.1 1.1 x 2 0.2 1.2 yAdd unnamed columns or mixin objects in the table using default names or by specifying explicit names with
names. Names can also be overridden:>>> t = Table() >>> col_b = Column(name='b', data=['u', 'v']) >>> t.add_columns([[1, 2], col_b]) >>> t.add_columns([[3, 4], col_b], names=['c', 'd']) >>> print(t) col0 b c d ---- --- --- --- 1 u 3 u 2 v 4 v