Pandas教程-数据连接
NumPy的concatenate
函数用于沿着行或列连接两个数组。它可以接受两个或更多形状相同的数组,默认按行连接,即axis=0
。
示例1:
# import numpy
import numpy as np
arr1 = np.arange(9)
arr1
arr2d_1 = array.reshape((3,3))
arr2d_1
arr2d_1 = np.arange(10,19).reshape(3,3)
arr2d_1
# concatenate 2 numpy arrays: row-wise
np.concatenate((arr2d_1, arr2d_2))
输出:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[10, 11, 12],
[13, 14, 15],
[16, 17, 18]])
示例2:
import pandas as pd
one = pd.DataFrame({'Name': ['Parker', 'Phill', 'Smith'],'id':[108,119,127]},index=['A','B','C'])
two = pd.DataFrame({'Name': ['Terry', 'Jones', 'John'],
'id':[102,125,112]},
index=['A','B','C'])
print(pd.concat([one,two]))
输出:
Name id
A Parker 108
B Phill 119
C Smith 127
A Terry 102
B Jones 125
C John 112
示例3:
import pandas as pd
one = pd.DataFrame({'Name': ['Parker', 'Phill', 'Smith'],'id':[108,119,127]},index=['A','B','C'])
two = pd.DataFrame({'Name': ['Terry', 'Jones', 'John'],
'id':[102,125,112]},
index=['A','B','C'])
print(pd.concat([one,two],keys=['x','y']))
输出:
Name id
x A Parker 108
B Phill119
C Smith 127
y A Terry 102
B Jones 125
C John 112