如果有多个结构一样的表,Concat函数就可以显示他的威力了!
表一:
图片.png
表二:
图片.png
代码:
import pandas as pd
path = 'data/Concat函数.xlsx'
df_sheet1 = pd.read_excel(path, sheet_name='Sheet1')
df_sheet2 = pd.read_excel(path, sheet_name='Sheet2')
df_new = pd.concat([df_sheet1, df_sheet2])
print(df_new)
输出:
user_id user_name
0 1 A
1 2 B
2 3 C
3 4 D
4 5 E
0 6 F
1 7 G
2 8 H
3 9 I
4 10 J
简直不要太方便!
如果两个表出现不匹配的怎么办呢?
实际上Concat函数有个join参数,默认时join="outer"外连接,这样就把所有的列合并过来了,如下图:
图片.png
图片.png
输出:
user_id user_name other1 other2
0 1 A a NaN
1 2 B b NaN
2 3 C c NaN
3 4 D d NaN
4 5 E e NaN
0 6 F NaN f
1 7 G NaN g
2 8 H NaN h
3 9 I NaN i
4 10 J NaN j
如果我只想要匹配到的列怎么办呢?那就用内连接”inner“:
import pandas as pd
path = 'data/Concat函数.xlsx'
df_sheet1 = pd.read_excel(path, sheet_name='Sheet1')
df_sheet2 = pd.read_excel(path, sheet_name='Sheet2')
df_new = pd.concat([df_sheet1, df_sheet2], join='inner')
print(df_new)
当文件夹里有多个结构相同的表格,可以这样操作:
import pandas as pd
import os
path = 'data/' # 数据存放文件夹
data_ls = []
for data_path in os.listdir(path):
df = pd.read_excel(io=path + data_path, sheet_name='Sheet1')
data_ls.append(df)
df_new = pd.concat(data_ls)
df_new.to_excel('a.xlsx', index=False)
网友评论