Pandas合并之Concat合并

1.Concat语法

pd.concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False,keys=None, levels=None, names=None, verify_integrity=False)

参数说明

objs:series或者dataframe对象构成的序列

asix:需要合并连接的轴,0是行,1是列

join:连接的方式,inner内连接,outer外连接

其它不常用参数......

2.Concat范例

(1)Series对象

1>Series对象的合并,横向合并纵向合并

#创建两个对象
ser_1 = pd.Series(np.random.randint(0,5,5),index=np.arange(5))
print(ser_1)
ser_2 = pd.Series(np.random.randint(5,10,5),index=np.arange(5))
print(ser_2)
'''
0    1
1    0
2    4
3    0
4    4
dtype: int32
0    7
1    9
2    6
3    5
4    5
dtype: int32
'''
#axis默认为0,是横向连接,返回一个series对象
ser = pd.concat([ser_1,ser_2])
print(ser)
'''
0    1
1    0
2    4
3    0
4    4
0    7
1    9
2    6
3    5
4    5
dtype: int32
'''

2>Series对象的合并,纵向合并,使用内连接

ser_1 = pd.Series(np.random.randint(0,10,5),index=range(5))
ser_2 = pd.Series(np.random.randint(0,10,4),index=range(4))
ser_3 = pd.Series(np.random.randint(0,10,3),index=range(3))
print(ser_1)
'''
0    4
1    3
2    6
3    4
4    6
dtype: int32
'''
print(ser_2)
'''
0    8
1    9
2    9
3    5
dtype: int32
'''
print(ser_3)
'''
0    6
1    0
2    3
dtype: int32
'''
ser = pd.concat([ser_1,ser_2,ser_3],axis=1,join='inner')
print(ser)
'''
dtype: int32
   0  1  2
0  4  8  6
1  3  9  0
2  6  9  3
'''

3>Series对象的合并,纵向合并,使用外连接

ser_1 = pd.Series(np.random.randint(0,10,5),index=range(5))
ser_2 = pd.Series(np.random.randint(0,10,4),index=range(4))
ser_3 = pd.Series(np.random.randint(0,10,3),index=range(3))
print(ser_1)
'''
0    2
1    7
2    6
3    7
4    3
dtype: int32
'''
print(ser_2)
'''
0    7
1    4
2    7
3    0
dtype: int32
'''
print(ser_3)
'''
0    3
1    3
2    5
dtype: int32
'''
ser = pd.concat([ser_1,ser_2,ser_3],axis=1,join='outer')
print(ser)
#返回一个DataFrame对象,并且取并集,对应位置没有数据自动填充Nan
'''
   0    1    2
0  2  7.0  3.0
1  7  4.0  3.0
2  6  7.0  5.0
3  7  0.0  NaN
4  3  NaN  NaN
'''

(2)DataFrame对象

1>DataFrame对象的合并,axis=0

# 创建两个DataFrame对象
df_1 = pd.DataFrame(np.random.randint(0,10,(3,2)),index=['a','b','c'],columns=['A','B'])
df_2 = pd.DataFrame(np.random.randint(0,10,(2,2)),index=['a','b'],columns=['C','D'])
print(df_1)
'''
   A  B
a  7  0
b  0  8
c  9  3
'''
print(df_2)
'''
   C  D
a  7  5
b  4  0
'''
df = pd.concat([df_1,df_2],axis=0)
print(df)
'''
     A    B    C    D
a  9.0  9.0  NaN  NaN
b  0.0  3.0  NaN  NaN
c  4.0  7.0  NaN  NaN
a  NaN  NaN  1.0  3.0
b  NaN  NaN  2.0  8.0
'''

1>DataFrame对象的合并,axis=1

# 创建两个DataFrame对象
df_1 = pd.DataFrame(np.random.randint(0,10,(3,2)),index=['a','b','c'],columns=['A','B'])
df_2 = pd.DataFrame(np.random.randint(0,10,(2,2)),index=['a','b'],columns=['C','D'])
print(df_1)
'''
   A  B
a  5  6
b  8  9
c  4  7
'''
print(df_2)
'''
   C  D
a  5  4
b  2  0
'''
df = pd.concat([df_1,df_2],axis=1)
print(df)
'''
   A  B    C    D
a  5  6  5.0  4.0
b  8  9  2.0  0.0
c  4  7  NaN  NaN
'''

(3)ndarray对象

import numpy as np
import pandas as pd

arr_1 = np.random.randint(0,10,(3,4))
arr_2 = np.random.randint(0,10,(3,4))
print(arr_1)
'''
[[1 8 7 9]
 [7 5 5 3]
 [0 7 5 7]]
'''
print(arr_2)
'''
[[8 8 2 9]
 [5 9 5 8]
 [3 1 6 5]]
'''
# concatenate 函数  合并的时候有轴向
arr = np.concatenate([arr_1,arr_2],axis=0)
print(arr)
'''
[[1 8 7 9]
 [7 5 5 3]
 [0 7 5 7]
 [8 8 2 9]
 [5 9 5 8]
 [3 1 6 5]]
'''
arr = np.concatenate([arr_1,arr_2],axis=1)
print(arr)
'''
[[1 8 7 9 8 8 2 9]
 [7 5 5 3 5 9 5 8]
 [0 7 5 7 3 1 6 5]]
'''






猜你喜欢

转载自blog.csdn.net/baoshuowl/article/details/79866589