datascience.tables.Table.join¶
-
Table.
join
(column_label, other, other_label=None)[source]¶ Creates a new table with the columns of self and other, containing rows for all values of a column that appear in both tables.
- Args:
column_label
(str
): label of column in self that is used to- join rows of
other
. other
: Table object to join with self on matching values ofcolumn_label
.
- Kwargs:
other_label
(str
): default None, assumescolumn_label
.- Otherwise in
other
used to join rows.
- Returns:
- New table self joined with
other
by matching values incolumn_label
andother_label
. If the resulting join is empty, returns None. If a join value appears more than once inself
, each row with that value will appear in resulting join, but inother
, only the first row with that value will be used.
>>> table = Table().with_columns('a', make_array(9, 3, 3, 1), ... 'b', make_array(1, 2, 2, 10), ... 'c', make_array(3, 4, 5, 6)) >>> table a | b | c 9 | 1 | 3 3 | 2 | 4 3 | 2 | 5 1 | 10 | 6 >>> table2 = Table().with_columns( 'a', make_array(9, 1, 1, 1), ... 'd', make_array(1, 2, 2, 10), ... 'e', make_array(3, 4, 5, 6)) >>> table2 a | d | e 9 | 1 | 3 1 | 2 | 4 1 | 2 | 5 1 | 10 | 6 >>> table.join('a', table2) a | b | c | d | e 1 | 10 | 6 | 2 | 4 9 | 1 | 3 | 1 | 3 >>> table.join('a', table2, 'a') # Equivalent to previous join a | b | c | d | e 1 | 10 | 6 | 2 | 4 9 | 1 | 3 | 1 | 3 >>> table.join('a', table2, 'd') # Repeat column labels relabeled a | b | c | a_2 | e 1 | 10 | 6 | 9 | 3 >>> table2 #table2 has three rows with a = 1 a | d | e 9 | 1 | 3 1 | 2 | 4 1 | 2 | 5 1 | 10 | 6 >>> table #table has only one row with a = 1 a | b | c 9 | 1 | 3 3 | 2 | 4 3 | 2 | 5 1 | 10 | 6 >>> table2.join('a', table) # When we join, we get all three rows in table2 where a = 1 a | d | e | b | c 1 | 2 | 4 | 10 | 6 1 | 2 | 5 | 10 | 6 1 | 10 | 6 | 10 | 6 9 | 1 | 3 | 1 | 3 >>> table.join('a', table2) # Opposite join only keeps first row in table2 with a = 1 a | b | c | d | e 1 | 10 | 6 | 2 | 4 9 | 1 | 3 | 1 | 3