Pandas quickly merges multiple excel sheets

Table of contents

1. When there are rules in the naming of Excel tables

2. When the file name of the Excel table is irregular

1. First put all the excel tables under a folder

2. Merge with pandas

3. Solution to merging garbled characters of ID number


Recently, I used Pandas to merge tables and found that the efficiency is very high. Now I summarize the following two methods:

1. When there are rules in the naming of Excel tables

import pandas as pd
path = 'C:/Users/admin/Downloads/'  #excel表格所在文件路径
df = [] #创建一个空的列表
for i in range(1,11):   #excel表格的名字里面包含1-10
    path = path + 'excel表格名称{}.xlsx'.format(i)
    df.append(pd.read_excel(path))  #读取文件夹下所有excel文件
df_concat = pd.concat(df) #excel文件合并
df_concat.to_excel('C:/Users/admin/Downloads/合并结果.xlsx',index=None) #合并结果存储

2. When the file name of the Excel table is irregular

1. First put all the excel tables under a folder

2. Merge with pandas

import pandas as pd
import os
path = 'C:/Users/admin/Downloads/'  #excel表格所在文件路径
df = [] #创建一个空的列表
for filename in os.listdir(dirs):   #获取文件夹下所有excel表格
    if filename.endwith('.xlsx'):
        path = dirs+filename
        df.append(pd.read_excel(path))  #读取文件夹下所有excel文件
df_concat = pd.concat(df) #excel文件合并
df_concat.to_excel(dirs+'{}.xlsx'.format('合并结果'),index=None) #合并结果存储

3. Solution to merging garbled characters of ID number

When merging ID numbers, garbled characters will appear due to the encoding method, which cannot be resolved through the encoding method in the form storage process. The following provides a perfect solution.

data = pd.read_excel('C:/Users/admin/Downloads/file.xlsx',index=None,converters={'身份证号码':str})
#data['身份证号码'] = data['身份证号码'].apply(lambda x:"\t"+x)
data.to_excel('需要保存的文件路径/file.xlsx',encoding='gbk',index=None)

Guess you like

Origin blog.csdn.net/weixin_43734080/article/details/127885238