In [ ]:
# Merging, Joining, and Concatenating
Example DataFrames¶
In [3]:
import pandas as pd
In [4]:
df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3'],
'C': ['C0', 'C1', 'C2', 'C3'],
'D': ['D0', 'D1', 'D2', 'D3']},
index=[0, 1, 2, 3])
In [5]:
df2 = pd.DataFrame({'A': ['A4', 'A5', 'A6', 'A7'],
'B': ['B4', 'B5', 'B6', 'B7'],
'C': ['C4', 'C5', 'C6', 'C7'],
'D': ['D4', 'D5', 'D6', 'D7']},
index=[4, 5, 6, 7])
In [6]:
df3 = pd.DataFrame({'A': ['A8', 'A9', 'A10', 'A11'],
'B': ['B8', 'B9', 'B10', 'B11'],
'C': ['C8', 'C9', 'C10', 'C11'],
'D': ['D8', 'D9', 'D10', 'D11']},
index=[8, 9, 10, 11])
In [7]:
df1
Out[7]:
In [8]:
df2
Out[8]:
In [12]:
df3
Out[12]:
In [ ]:
## Concatenation
In [10]:
pd.concat([df1,df2,df3])
Out[10]:
In [18]:
pd.concat([df1,df2,df3],axis=1)
Out[18]:
Example DataFrames¶
In [28]:
left = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],
'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3']})
right = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],
'C': ['C0', 'C1', 'C2', 'C3'],
'D': ['D0', 'D1', 'D2', 'D3']})
In [29]:
left
Out[29]:
In [30]:
right
Out[30]:
In [ ]:
## Merging
In [35]:
pd.merge(left,right,how='inner',on='key')
Out[35]:
Or to show a more complicated example:
In [37]:
['left = pd.DataFrame({'key1': ['K0', 'K0', 'K1', 'K2'],
'key2': ['K0', 'K1', 'K0', 'K1'],
'A': ['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3']})
right = pd.DataFrame({'key1': ['K0', 'K1', 'K1', 'K2'],
'key2': ['K0', 'K0', 'K0', 'K0'],
'C': ['C0', 'C1', 'C2', 'C3'],
'D': ['D0', 'D1', 'D2', 'D3']})']
In [39]:
pd.merge(left, right, on=['key1', 'key2'])
Out[39]:
In [40]:
pd.merge(left, right, how='outer', on=['key1', 'key2'])
Out[40]:
In [41]:
pd.merge(left, right, how='right', on=['key1', 'key2'])
Out[41]:
In [42]:
pd.merge(left, right, how='left', on=['key1', 'key2'])
Out[42]:
In [ ]:
## Joining
In [46]:
left = pd.DataFrame({'A': ['A0', 'A1', 'A2'],
'B': ['B0', 'B1', 'B2']},
index=['K0', 'K1', 'K2'])
right = pd.DataFrame({'C': ['C0', 'C2', 'C3'],
'D': ['D0', 'D2', 'D3']},
index=['K0', 'K2', 'K3'])
In [47]:
left.join(right)
Out[47]:
In [48]:
left.join(right, how='outer')
Out[48]:
No comments:
Post a Comment