(1)使用concat函数实现数据堆叠
①对表3和表4外连接横向堆叠,并使用shape函数统计新数据框的大小;
②对表3和表4内连接横向堆叠,并使用shape函数统计新数据框的大小;
③对表3和表4外连接纵向堆叠,并使用shape函数统计新数据框的大小;
④对表3和表4内连接纵向堆叠,并使用shape函数统计新数据框的大小;
表3 DataFrame
col1 | col2 | col3 | col4 | |
1 | a | 0 | 1 | 2 |
2 | b | 3 | 4 | 8 |
3 | c | 6 | 7 | 5 |
5 | g | 9 | 10 | 11 |
表4 DataFrame
col2 | col4 | col5 | |
4 | f | 7 | 2 |
5 | d | 4 | 7 |
6 | e | 8 | 15 |
建立表格
import pandas as pd
d = {'col1': ['a','b','c','g'], 'col2': [0,3,6,9], 'col3': [1,4,7,10],'col4':[2,8,5,11]}
df3 = pd.DataFrame(d,index = [1,2,3,5])
print(df3)
d = {'col2': ['f','d','e'], 'col4': [7,4,8], 'col5': [2,7,15]}
df4 = pd.DataFrame(d,index = [4,5,6])
print(df4)
以外连接纵向堆叠为例
df5 = pd.concat([df3,df4],join = 'outer',axis = 0)
print(df5)
df5.shape #使用shape函数统计新数据框的大小
运行结果