在實際應(yīng)用中秃踩,pandas的concat/merge方法以及groupby方法用的比較多判导,這里通過實例進(jìn)行簡要的介紹恃锉,本次內(nèi)容主要介紹合并方法棺牧。
內(nèi)容翻譯自:http://pandas.pydata.org/pandas-docs/stable/merging.html#merging
concat
先來看一下concat方法的原型
pd.concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False,
keys=None, levels=None, names=None, verify_integrity=False,
copy=True)
只看前三個:
- objs: series,dataframe或者是panel構(gòu)成的序列l(wèi)sit
- axis: 需要合并的軸犹菱,0是行拾稳,1是列
- join:連接的方式 inner,或者outer
例1:
In [1]: 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 [2]: 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 [3]: 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 [4]: frames = [df1, df2, df3]
In [5]: result = pd.concat(frames)
如果需要指明具體的數(shù)據(jù)來自哪一個“分片”腊脱,可以加入keys屬性访得,例2:
In [6]: result = pd.concat(frames, keys=['x', 'y', 'z'])
可以看到結(jié)果多了一個分層索引,即最左邊一列陕凹,我們可以通過以下方式對其引用:
In [7]: result.loc['y']
Out[7]:
A B C D
4 A4 B4 C4 D4
5 A5 B5 C5 D5
6 A6 B6 C6 D6
7 A7 B7 C7 D7
當(dāng)然也可以加上axis屬性悍抑,即確定合并的具體軸,例3:
In [8]: df4 = pd.DataFrame({'B': ['B2', 'B3', 'B6', 'B7'],
...: 'D': ['D2', 'D3', 'D6', 'D7'],
...: 'F': ['F2', 'F3', 'F6', 'F7']},
...: index=[2, 3, 6, 7])
...:
In [9]: result = pd.concat([df1, df4], axis=1)
可以看到此時jion的方式為outer杜耙,在并集上原來沒有元素的片上用np.nan填充搜骡,這也是pandas的一致做法。
同樣可以指定join的方式佑女,inner交集 or outer并集记靡,例4:
In [10]: result = pd.concat([df1, df4], axis=1, join='inner')
最后我們當(dāng)然也可以指定具體用哪個分片的index,例5:
In [11]: result = pd.concat([df1, df4], axis=1, join_axes=[df1.index])
merge
首先看一下merge函數(shù)的原型
pd.merge(left, right, how='inner', on=None, left_on=None, right_on=None,
left_index=False, right_index=False, sort=True,
suffixes=('_x', '_y'), copy=True, indicator=False,
validate=None)
- left团驱,right:需要merge的DataFrame對象
- how:jion的方式摸吠,inner(默認(rèn)),outer嚎花,left(左邊對象的key)寸痢,或者right
- on:jion用來對齊的那一列的名字
首先來看一個簡單的例子,例6:
In [38]: left = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],
....: 'A': ['A0', 'A1', 'A2', 'A3'],
....: 'B': ['B0', 'B1', 'B2', 'B3']})
....:
In [39]: right = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],
....: 'C': ['C0', 'C1', 'C2', 'C3'],
....: 'D': ['D0', 'D1', 'D2', 'D3']})
....:
In [40]: result = pd.merge(left, right, on='key')
也可以指定多個jion key紊选,例7:
In [41]: left = pd.DataFrame({'key1': ['K0', 'K0', 'K1', 'K2'],
....: 'key2': ['K0', 'K1', 'K0', 'K1'],
....: 'A': ['A0', 'A1', 'A2', 'A3'],
....: 'B': ['B0', 'B1', 'B2', 'B3']})
....:
In [42]: right = pd.DataFrame({'key1': ['K0', 'K1', 'K1', 'K2'],
....: 'key2': ['K0', 'K0', 'K0', 'K0'],
....: 'C': ['C0', 'C1', 'C2', 'C3'],
....: 'D': ['D0', 'D1', 'D2', 'D3']})
....:
In [43]: result = pd.merge(left, right, on=['key1', 'key2'])
也可以通過how指定merge的方式啼止,例8:
In [44]: result = pd.merge(left, right, how='left', on=['key1', 'key2'])
In [45]: result = pd.merge(left, right, how='inner', on=['key1', 'key2'])
In [46]: result = pd.merge(left, right, how='outer', on=['key1', 'key2'])
再來一個重復(fù)連接鍵的例子,例11:
In [48]: left = pd.DataFrame({'A' : [1,2], 'B' : [2, 2]})
In [49]: right = pd.DataFrame({'A' : [4,5,6], 'B': [2,2,2]})
In [50]: result = pd.merge(left, right, on='B', how='outer')