Pandas-20.級聯
連接配接對象
concat
函數可以将Series,DataFrame和Panel對象之間互相組合在一起
pd.concat(objs,axis=0,join='outer',join_axes=None,
ignore_index=False)
複制
- objs - 這是Series,DataFrame或Panel對象的序列或映射。
- axis - {0,1,...},預設為0,這是連接配接的軸。
- join - {'inner', 'outer'},預設inner。如何處理其他軸上的索引。聯合的外部和交叉的内部。
- ignore_index − 布爾值,預設為False。如果指定為True,則不要使用連接配接軸上的索引值。結果軸将被标記為:0,...,n-1。
- join_axes - 這是Index對象的清單。用于其他(n-1)軸的特定索引,而不是執行内部/外部集邏輯。
連接配接對象
- 連接配接兩個DataFrame
import pandas as pd
one = pd.DataFrame({
'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'],
'subject_id':['sub1','sub2','sub4','sub6','sub5'],
'Marks_scored':[98,90,87,69,78]},
index=[1,2,3,4,5])
two = pd.DataFrame({
'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'],
'subject_id':['sub2','sub4','sub3','sub6','sub5'],
'Marks_scored':[89,80,79,97,88]},
index=[1,2,3,4,5])
rs = pd.concat([one,two])
print(rs)
'''
Name subject_id Marks_scored
1 Alex sub1 98
2 Amy sub2 90
3 Allen sub4 87
4 Alice sub6 69
5 Ayoung sub5 78
1 Billy sub2 89
2 Brian sub4 80
3 Bran sub3 79
4 Bryce sub6 97
5 Betty sub5 88
'''
複制
指定特定的鍵與關聯每個切片的DataFrame,用keys參數:
print(pd.concat([one,two],keys=['x','y']))
'''
Name subject_id Marks_scored
x 1 Alex sub1 98
2 Amy sub2 90
3 Allen sub4 87
4 Alice sub6 69
5 Ayoung sub5 78
y 1 Billy sub2 89
2 Brian sub4 80
3 Bran sub3 79
4 Bryce sub6 97
5 Betty sub5 88
'''
複制
重複索引用
ignore_index
參數設定為True來強制設定對象遵循自己的索引(索引被改變,keys被覆寫):
print(pd.concat([one,two], keys=['X', 'Y'], ignore_index=True))
'''
Name subject_id Marks_scored
0 Alex sub1 98
1 Amy sub2 90
2 Allen sub4 87
3 Alice sub6 69
4 Ayoung sub5 78
5 Billy sub2 89
6 Brian sub4 80
7 Bran sub3 79
8 Bryce sub6 97
9 Betty sub5 88
'''
複制
如果axis=1,會添加新列:
print(pd.concat([one,two],axis=1))
'''
Name subject_id Marks_scored Name subject_id Marks_scored
1 Alex sub1 98 Billy sub2 89
2 Amy sub2 90 Brian sub4 80
3 Allen sub4 87 Bran sub3 79
4 Alice sub6 69 Bryce sub6 97
5 Ayoung sub5 78 Betty sub5 88
'''
複制
使用append連接配接
還有一個簡單的連接配接的方法用
append()
。
print(one.append(two))
'''
Name subject_id Marks_scored
1 Alex sub1 98
2 Amy sub2 90
3 Allen sub4 87
4 Alice sub6 69
5 Ayoung sub5 78
1 Billy sub2 89
2 Brian sub4 80
3 Bran sub3 79
4 Bryce sub6 97
5 Betty sub5 88
'''
複制
append()
函數可以帶多個對象:
print(one.append([two,one]))
'''
Name subject_id Marks_scored
1 Alex sub1 98
2 Amy sub2 90
3 Allen sub4 87
4 Alice sub6 69
5 Ayoung sub5 78
1 Billy sub2 89
2 Brian sub4 80
3 Bran sub3 79
4 Bryce sub6 97
5 Betty sub5 88
1 Alex sub1 98
2 Amy sub2 90
3 Allen sub4 87
4 Alice sub6 69
5 Ayoung sub5 78
'''
複制