很多时候
我们需要n个dataframe进行合并
或者上下拼接行
或者左右拼接列
这里就两种方法做些笔记:
pandas.DataFrame.append
官方说明:
将data以添加行的方式添加到原data,并返回一个新的对象
Append rows of other to the end of caller, returning a new object.
如果列不重合,则以新列的方式添加
Columns in other that are not in the caller are added as new columns.
>>> df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
>>> df
A B
0 1 2
1 3 4
>>> df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'))
>>> df.append(df2)
A B
0 1 2
1 3 4
0 5 6
1 7 8
忽略行的索引的话 ignore_index=True`
>>> df.append(df2, ignore_index=True)
A B
0 1 2
1 3 4
2 5 6
3 7 8
pandas.DataFrame.join
官方说明
将新数据以列的方式添加
Join columns of another DataFrame.
>>> df = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3', 'K4', 'K5'],
... 'A': ['A0', 'A1', 'A2', 'A3', 'A4', 'A5']})
>>> df
key A
0 K0 A0
1 K1 A1
2 K2 A2
3 K3 A3
4 K4 A4
5 K5 A5
>>> other = pd.DataFrame({'key': ['K0', 'K1', 'K2'],
...
>>> other
key B
0 K0 B0
1 K1 B1
2 K2 B2
>>> df.set_index('key').join(other.set_index('key'))
A B
key
K0 A0 B0
K1 A1 B1
K2 A2 B2
K3 A3 NaN
K4 A4 NaN
K5 A5 NaN
或者
>>> df.join(other.set_index('key'), on='key')
key A B
0 K0 A0 B0
1 K1 A1 B1
2 K2 A2 B2
3 K3 A3 NaN
4 K4 A4 NaN
5 K5 A5 NaN