Given two data tables: left and right, they have at least one column with the same name. The name is assumed to be a key. How to connect these two tables?
Python provides a merge operation to accomplish this.
The keys of the left table are: k0, k1, k2; the keys of the right table are: k0, k0, k1
**1) **If the key ** of the left table points to the ** right table, then this connection method is called how: left: The relationship diagram is expressed as follows:
left right
k0 k0
k0
k1 k1
k2 NaN
**2) **If the key ** of the right table points to the ** left table, then this connection method is called how: right
right left
k0 k0
k0
k1 k1
**3) **If only the shared key of the left and right tables is used to establish a relationship, then this connection method is called how: inner
left right
k0 k0
k0 k0
k1 k1
**4) **If a relationship is established after the keys of the left and right tables are merged, the connection method is called how: outer
left right
k0 k0
k0
k1 k1
k2 NaN
**The above are the 4 ways to establish a relationship based on the key node when connecting two tables in merge. **
The following example is verified
Left and right table data:
left
age1 key
010 k0
120 k1
230 k2
right
age2 key
040 k0
150 k0
260 k1
**how = 'left' **
pd.merge(left,right,how='left',on='key')
age1 key age2
010 k0 40.0110 k0 50.0220 k1 60.0330 k2 NaN
**how = 'right' **
pd.merge(left,right,how='right',on='key')
age1 key age2
010 k0 40110 k0 50220 k1 60
how = 'inner'
pd.merge(left,right,how='inner',on='key')
age1 key age2
010 k0 40110 k0 50220 k1 60
how = 'outer'
pd.merge(left,right,how='outer',on='key')
age1 key age2
010 k0 40.0110 k0 50.0220 k1 60.0330 k2 NaN
**- - - - - - - - - - - - - - - - - - - - - ** End ---------------------
Recommended Posts