在实际应用中,pandas的concat/merge方法以及groupby方法用的比较多,这里通过实例进行简要的介绍,本次内容主要介绍合并方法。
内容翻译自: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构成的序列lsit
- 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)
如果需要指明具体的数据来自哪一个“分片”,可以加入keys属性,例2:
In [6]: result = pd.concat(frames, keys=['x', 'y', 'z'])
可以看到结果多了一个分层索引,即最左边一列,我们可以通过以下方式对其引用:
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
当然也可以加上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')
最后我们当然也可以指定具体用哪个分片的index,例5:
In [11]: result = pd.concat([df1, df4], axis=1, join_axes=[df1.index])
merge
首先看一下merge函数的原型
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(默认),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'])
再来一个重复连接键的例子,例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')