Python怎样用Pandas遍历目标文件夹下所有后缀,格式相同的表格数据并按指定条件筛选汇总到一个新的表格

本人是初入Python界的小白,可以说是白中白。因工作需要,批量对数据处理,Get到Python这项小技能格外重要。因避免涉及公司和个人隐私,故举个与本人无关的工作示例和代码,供大家参考,希望大家能交流互相学习。这是我第一篇CSDN博客。学习Python的大佬们都知道有2句非常有名的话语,1中1英。以后我的博客都会出现这2句话,以‘英’开始,以‘中’结束。

Talk is cheap,please show your code.
However,为了让大家更简单易懂代码结构和运行,还是上几张图解释一下。
1.目标目录下的表格文件(.xlsx)
在这里插入图片描述
2.随便打开一个表格看一下:(所有表格的格式是一致的)
在这里插入图片描述
3.目的:把目标文件夹下的所有.xlsx文件中Customer中为‘B’客户的都提取出来,做客户一致性筛查处理,时间起始是2020-3-20到2020-4-5,我们看到表格中是没有文件客户是谁负责的信息,所以我们要把提取的文件的名称加到各自的筛选数据中。OK,let us Go!
Now,Beginning!
Talk is cheap,show me your code.好吧我刚说过了。

import pandas as pd
import os
import re
import openpyxl
from openpyxl import load_workbook
import time
#人机交互,信息收集,判断输入数据的准确与否,选择日期范围,需要筛选的客户,可自行设置
start_date = input('请输入日期起始点:(格式1978/4/1)\n')
end_date = input('请输入日期终止点:(格式同上)\n')
if start_date > end_date:
    print('有误,请检查!')
    time.sleep(1)
    print('程序结束,请重新Running.')
else:
    Customer = input('请输入您要汇总的客户名称:(格式ABCD)\n')
    time.sleep(1)
    if Customer != 'A'and \
            Customer != 'B'and \
            Customer != 'C' and \
            Customer != 'D'and \
            Customer != 'E' and \
            Customer != 'F' and \
            Customer != 'G' and \
            Customer != 'H':
        print('有误,请检查!')
        time.sleep(1)
        print('程序结束,请重新Running.')
    else:
        print('信息获取完毕,数据正在提取\n')
        time.sleep(1)
        print('...\n......\n.........')
        #开始导入Timer,Running
        start = time.time()
        #读取路径到上层文件夹,因为要遍历该文件夹下所有的表格
        inpath = "C:/Users/Administrator/Desktop/Python/read/5"
        #输出目标路径
        outpath = "C:/Users/Administrator/Desktop/Python/write/5/客户一致性筛查.xlsx"
        #后缀定义
        suffix = '.xlsx'
        #核心代码
        file_list = os.listdir(inpath)
        print('在目标路径' + inpath + '\n找到后缀为' + suffix + '的文件:\n'+ str(file_list))
        #定义数据框
        df_raw= pd.DataFrame()
        for file in file_list:
            if file.endswith(suffix):
                path = os.path.join(inpath,file)
                #正则提取文件名称
                order_name = re.findall(r"(.+?)\.",file)
                #读取表格数据
                df0 = pd.read_excel(path)
                #日期筛选及客户筛选
                df0['Date'] = df0['Date'].apply(pd.to_datetime,format='%Y-%m-%d')#具体时间:%H:%S:%f
                df00 = df0.loc[(df0['Date'] >= start_date) & (df0['Date'] <= end_date) & (df0['Customer'] == Customer)]
                length = len(df00)
                order_name_col = order_name * length
                #加销售人员的名字
                df00.insert(1,"From",order_name_col)
                #拼接数据
                df_raw = pd.concat([df_raw,df00],axis=0,sort=False)
                print(df_raw)
        df_raw.to_excel(outpath)
        #停止计时
        end = time.time()
        print('本次任务用时'+str(round(end-start,3))+'s')
        print('输出文件位置:\n'+outpath)

4.可以来看看运行过程:
在这里插入图片描述

C:\Users\Administrator\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/Administrator/PycharmProjects/untitled/CustomerTotal.py
请输入日期起始点:(格式1978/4/1)
2020/3/20
请输入日期终止点:(格式同上)
2020/4/5
请输入您要汇总的客户名称:(格式ABCD)
B
信息获取完毕,数据正在提取

...
......
.........
在目标路径C:/Users/Administrator/Desktop/Python/read/5
找到后缀为.xlsx的文件:
['Arry.xlsx', 'Bob.xlsx', 'Cathy.xlsx', 'Diavd.xlsx', 'Jessia.xlsx', 'John.xlsx', 'Lily.xlsx', 'Michcal.xlsx', 'Oliver.xlsx', 'Rebbita.xlsx', 'Sabria.xlsx']
         Date  From Customer Address Requirment     Contact
10 2020-03-22  Arry        B      上海      2层阳光板  1864567343
13 2020-03-25  Arry        B      上海      4层阳关板  1864567346
18 2020-03-30  Arry        B      上海        阳光板  1874567342
24 2020-04-05  Arry        B      上海        阳光板  1834567341
         Date  From Customer Address Requirment     Contact
10 2020-03-22  Arry        B      上海      2层阳光板  1864567343
13 2020-03-25  Arry        B      上海      4层阳关板  1864567346
18 2020-03-30  Arry        B      上海        阳光板  1874567342
24 2020-04-05  Arry        B      上海        阳光板  1834567341
10 2020-03-22   Bob        B      上海      2层阳光板  1864567343
13 2020-03-25   Bob        B      上海      4层阳关板  1864567346
18 2020-03-30   Bob        B      上海        阳光板  1874567342
24 2020-04-05   Bob        B      上海        阳光板  1834567341
         Date   From Customer Address Requirment     Contact
10 2020-03-22   Arry        B      上海      2层阳光板  1864567343
13 2020-03-25   Arry        B      上海      4层阳关板  1864567346
18 2020-03-30   Arry        B      上海        阳光板  1874567342
24 2020-04-05   Arry        B      上海        阳光板  1834567341
10 2020-03-22    Bob        B      上海      2层阳光板  1864567343
13 2020-03-25    Bob        B      上海      4层阳关板  1864567346
18 2020-03-30    Bob        B      上海        阳光板  1874567342
24 2020-04-05    Bob        B      上海        阳光板  1834567341
10 2020-03-22  Cathy        B      上海      2层阳光板  1864567343
13 2020-03-25  Cathy        B      上海      4层阳关板  1864567346
18 2020-03-30  Cathy        B      上海        阳光板  1874567342
24 2020-04-05  Cathy        B      上海        阳光板  1834567341
         Date   From Customer Address Requirment     Contact
10 2020-03-22   Arry        B      上海      2层阳光板  1864567343
13 2020-03-25   Arry        B      上海      4层阳关板  1864567346
18 2020-03-30   Arry        B      上海        阳光板  1874567342
24 2020-04-05   Arry        B      上海        阳光板  1834567341
10 2020-03-22    Bob        B      上海      2层阳光板  1864567343
13 2020-03-25    Bob        B      上海      4层阳关板  1864567346
18 2020-03-30    Bob        B      上海        阳光板  1874567342
24 2020-04-05    Bob        B      上海        阳光板  1834567341
10 2020-03-22  Cathy        B      上海      2层阳光板  1864567343
13 2020-03-25  Cathy        B      上海      4层阳关板  1864567346
18 2020-03-30  Cathy        B      上海        阳光板  1874567342
24 2020-04-05  Cathy        B      上海        阳光板  1834567341
10 2020-03-22  Diavd        B      上海      2层阳光板  1864567343
13 2020-03-25  Diavd        B      上海      4层阳关板  1864567346
18 2020-03-30  Diavd        B      上海        阳光板  1874567342
24 2020-04-05  Diavd        B      上海        阳光板  1834567341
         Date    From Customer Address Requirment     Contact
10 2020-03-22    Arry        B      上海      2层阳光板  1864567343
13 2020-03-25    Arry        B      上海      4层阳关板  1864567346
18 2020-03-30    Arry        B      上海        阳光板  1874567342
24 2020-04-05    Arry        B      上海        阳光板  1834567341
10 2020-03-22     Bob        B      上海      2层阳光板  1864567343
13 2020-03-25     Bob        B      上海      4层阳关板  1864567346
18 2020-03-30     Bob        B      上海        阳光板  1874567342
24 2020-04-05     Bob        B      上海        阳光板  1834567341
10 2020-03-22   Cathy        B      上海      2层阳光板  1864567343
13 2020-03-25   Cathy        B      上海      4层阳关板  1864567346
18 2020-03-30   Cathy        B      上海        阳光板  1874567342
24 2020-04-05   Cathy        B      上海        阳光板  1834567341
10 2020-03-22   Diavd        B      上海      2层阳光板  1864567343
13 2020-03-25   Diavd        B      上海      4层阳关板  1864567346
18 2020-03-30   Diavd        B      上海        阳光板  1874567342
24 2020-04-05   Diavd        B      上海        阳光板  1834567341
10 2020-03-22  Jessia        B      上海      2层阳光板  1864567343
13 2020-03-25  Jessia        B      上海      4层阳关板  1864567346
18 2020-03-30  Jessia        B      上海        阳光板  1874567342
24 2020-04-05  Jessia        B      上海        阳光板  1834567341
         Date    From Customer Address Requirment     Contact
10 2020-03-22    Arry        B      上海      2层阳光板  1864567343
13 2020-03-25    Arry        B      上海      4层阳关板  1864567346
18 2020-03-30    Arry        B      上海        阳光板  1874567342
24 2020-04-05    Arry        B      上海        阳光板  1834567341
10 2020-03-22     Bob        B      上海      2层阳光板  1864567343
13 2020-03-25     Bob        B      上海      4层阳关板  1864567346
18 2020-03-30     Bob        B      上海        阳光板  1874567342
24 2020-04-05     Bob        B      上海        阳光板  1834567341
10 2020-03-22   Cathy        B      上海      2层阳光板  1864567343
13 2020-03-25   Cathy        B      上海      4层阳关板  1864567346
18 2020-03-30   Cathy        B      上海        阳光板  1874567342
24 2020-04-05   Cathy        B      上海        阳光板  1834567341
10 2020-03-22   Diavd        B      上海      2层阳光板  1864567343
13 2020-03-25   Diavd        B      上海      4层阳关板  1864567346
18 2020-03-30   Diavd        B      上海        阳光板  1874567342
24 2020-04-05   Diavd        B      上海        阳光板  1834567341
10 2020-03-22  Jessia        B      上海      2层阳光板  1864567343
13 2020-03-25  Jessia        B      上海      4层阳关板  1864567346
18 2020-03-30  Jessia        B      上海        阳光板  1874567342
24 2020-04-05  Jessia        B      上海        阳光板  1834567341
10 2020-03-22    John        B      上海      2层阳光板  1864567343
13 2020-03-25    John        B      上海      4层阳关板  1864567346
18 2020-03-30    John        B      上海        阳光板  1874567342
24 2020-04-05    John        B      上海        阳光板  1834567341
         Date    From Customer Address Requirment     Contact
10 2020-03-22    Arry        B      上海      2层阳光板  1864567343
13 2020-03-25    Arry        B      上海      4层阳关板  1864567346
18 2020-03-30    Arry        B      上海        阳光板  1874567342
24 2020-04-05    Arry        B      上海        阳光板  1834567341
10 2020-03-22     Bob        B      上海      2层阳光板  1864567343
13 2020-03-25     Bob        B      上海      4层阳关板  1864567346
18 2020-03-30     Bob        B      上海        阳光板  1874567342
24 2020-04-05     Bob        B      上海        阳光板  1834567341
10 2020-03-22   Cathy        B      上海      2层阳光板  1864567343
13 2020-03-25   Cathy        B      上海      4层阳关板  1864567346
18 2020-03-30   Cathy        B      上海        阳光板  1874567342
24 2020-04-05   Cathy        B      上海        阳光板  1834567341
10 2020-03-22   Diavd        B      上海      2层阳光板  1864567343
13 2020-03-25   Diavd        B      上海      4层阳关板  1864567346
18 2020-03-30   Diavd        B      上海        阳光板  1874567342
24 2020-04-05   Diavd        B      上海        阳光板  1834567341
10 2020-03-22  Jessia        B      上海      2层阳光板  1864567343
13 2020-03-25  Jessia        B      上海      4层阳关板  1864567346
18 2020-03-30  Jessia        B      上海        阳光板  1874567342
24 2020-04-05  Jessia        B      上海        阳光板  1834567341
10 2020-03-22    John        B      上海      2层阳光板  1864567343
13 2020-03-25    John        B      上海      4层阳关板  1864567346
18 2020-03-30    John        B      上海        阳光板  1874567342
24 2020-04-05    John        B      上海        阳光板  1834567341
10 2020-03-22    Lily        B      上海      2层阳光板  1864567343
13 2020-03-25    Lily        B      上海      4层阳关板  1864567346
18 2020-03-30    Lily        B      上海        阳光板  1874567342
24 2020-04-05    Lily        B      上海        阳光板  1834567341
         Date     From Customer Address Requirment     Contact
10 2020-03-22     Arry        B      上海      2层阳光板  1864567343
13 2020-03-25     Arry        B      上海      4层阳关板  1864567346
18 2020-03-30     Arry        B      上海        阳光板  1874567342
24 2020-04-05     Arry        B      上海        阳光板  1834567341
10 2020-03-22      Bob        B      上海      2层阳光板  1864567343
13 2020-03-25      Bob        B      上海      4层阳关板  1864567346
18 2020-03-30      Bob        B      上海        阳光板  1874567342
24 2020-04-05      Bob        B      上海        阳光板  1834567341
10 2020-03-22    Cathy        B      上海      2层阳光板  1864567343
13 2020-03-25    Cathy        B      上海      4层阳关板  1864567346
18 2020-03-30    Cathy        B      上海        阳光板  1874567342
24 2020-04-05    Cathy        B      上海        阳光板  1834567341
10 2020-03-22    Diavd        B      上海      2层阳光板  1864567343
13 2020-03-25    Diavd        B      上海      4层阳关板  1864567346
18 2020-03-30    Diavd        B      上海        阳光板  1874567342
24 2020-04-05    Diavd        B      上海        阳光板  1834567341
10 2020-03-22   Jessia        B      上海      2层阳光板  1864567343
13 2020-03-25   Jessia        B      上海      4层阳关板  1864567346
18 2020-03-30   Jessia        B      上海        阳光板  1874567342
24 2020-04-05   Jessia        B      上海        阳光板  1834567341
10 2020-03-22     John        B      上海      2层阳光板  1864567343
13 2020-03-25     John        B      上海      4层阳关板  1864567346
18 2020-03-30     John        B      上海        阳光板  1874567342
24 2020-04-05     John        B      上海        阳光板  1834567341
10 2020-03-22     Lily        B      上海      2层阳光板  1864567343
13 2020-03-25     Lily        B      上海      4层阳关板  1864567346
18 2020-03-30     Lily        B      上海        阳光板  1874567342
24 2020-04-05     Lily        B      上海        阳光板  1834567341
10 2020-03-22  Michcal        B      上海      2层阳光板  1864567343
13 2020-03-25  Michcal        B      上海      4层阳关板  1864567346
18 2020-03-30  Michcal        B      上海        阳光板  1874567342
24 2020-04-05  Michcal        B      上海        阳光板  1834567341
         Date     From Customer Address Requirment     Contact
10 2020-03-22     Arry        B      上海      2层阳光板  1864567343
13 2020-03-25     Arry        B      上海      4层阳关板  1864567346
18 2020-03-30     Arry        B      上海        阳光板  1874567342
24 2020-04-05     Arry        B      上海        阳光板  1834567341
10 2020-03-22      Bob        B      上海      2层阳光板  1864567343
13 2020-03-25      Bob        B      上海      4层阳关板  1864567346
18 2020-03-30      Bob        B      上海        阳光板  1874567342
24 2020-04-05      Bob        B      上海        阳光板  1834567341
10 2020-03-22    Cathy        B      上海      2层阳光板  1864567343
13 2020-03-25    Cathy        B      上海      4层阳关板  1864567346
18 2020-03-30    Cathy        B      上海        阳光板  1874567342
24 2020-04-05    Cathy        B      上海        阳光板  1834567341
10 2020-03-22    Diavd        B      上海      2层阳光板  1864567343
13 2020-03-25    Diavd        B      上海      4层阳关板  1864567346
18 2020-03-30    Diavd        B      上海        阳光板  1874567342
24 2020-04-05    Diavd        B      上海        阳光板  1834567341
10 2020-03-22   Jessia        B      上海      2层阳光板  1864567343
13 2020-03-25   Jessia        B      上海      4层阳关板  1864567346
18 2020-03-30   Jessia        B      上海        阳光板  1874567342
24 2020-04-05   Jessia        B      上海        阳光板  1834567341
10 2020-03-22     John        B      上海      2层阳光板  1864567343
13 2020-03-25     John        B      上海      4层阳关板  1864567346
18 2020-03-30     John        B      上海        阳光板  1874567342
24 2020-04-05     John        B      上海        阳光板  1834567341
10 2020-03-22     Lily        B      上海      2层阳光板  1864567343
13 2020-03-25     Lily        B      上海      4层阳关板  1864567346
18 2020-03-30     Lily        B      上海        阳光板  1874567342
24 2020-04-05     Lily        B      上海        阳光板  1834567341
10 2020-03-22  Michcal        B      上海      2层阳光板  1864567343
13 2020-03-25  Michcal        B      上海      4层阳关板  1864567346
18 2020-03-30  Michcal        B      上海        阳光板  1874567342
24 2020-04-05  Michcal        B      上海        阳光板  1834567341
10 2020-03-22   Oliver        B      上海      2层阳光板  1864567343
13 2020-03-25   Oliver        B      上海      4层阳关板  1864567346
18 2020-03-30   Oliver        B      上海        阳光板  1874567342
24 2020-04-05   Oliver        B      上海        阳光板  1834567341
         Date     From Customer Address Requirment     Contact
10 2020-03-22     Arry        B      上海      2层阳光板  1864567343
13 2020-03-25     Arry        B      上海      4层阳关板  1864567346
18 2020-03-30     Arry        B      上海        阳光板  1874567342
24 2020-04-05     Arry        B      上海        阳光板  1834567341
10 2020-03-22      Bob        B      上海      2层阳光板  1864567343
13 2020-03-25      Bob        B      上海      4层阳关板  1864567346
18 2020-03-30      Bob        B      上海        阳光板  1874567342
24 2020-04-05      Bob        B      上海        阳光板  1834567341
10 2020-03-22    Cathy        B      上海      2层阳光板  1864567343
13 2020-03-25    Cathy        B      上海      4层阳关板  1864567346
18 2020-03-30    Cathy        B      上海        阳光板  1874567342
24 2020-04-05    Cathy        B      上海        阳光板  1834567341
10 2020-03-22    Diavd        B      上海      2层阳光板  1864567343
13 2020-03-25    Diavd        B      上海      4层阳关板  1864567346
18 2020-03-30    Diavd        B      上海        阳光板  1874567342
24 2020-04-05    Diavd        B      上海        阳光板  1834567341
10 2020-03-22   Jessia        B      上海      2层阳光板  1864567343
13 2020-03-25   Jessia        B      上海      4层阳关板  1864567346
18 2020-03-30   Jessia        B      上海        阳光板  1874567342
24 2020-04-05   Jessia        B      上海        阳光板  1834567341
10 2020-03-22     John        B      上海      2层阳光板  1864567343
13 2020-03-25     John        B      上海      4层阳关板  1864567346
18 2020-03-30     John        B      上海        阳光板  1874567342
24 2020-04-05     John        B      上海        阳光板  1834567341
10 2020-03-22     Lily        B      上海      2层阳光板  1864567343
13 2020-03-25     Lily        B      上海      4层阳关板  1864567346
18 2020-03-30     Lily        B      上海        阳光板  1874567342
24 2020-04-05     Lily        B      上海        阳光板  1834567341
10 2020-03-22  Michcal        B      上海      2层阳光板  1864567343
13 2020-03-25  Michcal        B      上海      4层阳关板  1864567346
18 2020-03-30  Michcal        B      上海        阳光板  1874567342
24 2020-04-05  Michcal        B      上海        阳光板  1834567341
10 2020-03-22   Oliver        B      上海      2层阳光板  1864567343
13 2020-03-25   Oliver        B      上海      4层阳关板  1864567346
18 2020-03-30   Oliver        B      上海        阳光板  1874567342
24 2020-04-05   Oliver        B      上海        阳光板  1834567341
10 2020-03-22  Rebbita        B      上海      2层阳光板  1864567343
13 2020-03-25  Rebbita        B      上海      4层阳关板  1864567346
18 2020-03-30  Rebbita        B      上海        阳光板  1874567342
24 2020-04-05  Rebbita        B      上海        阳光板  1834567341
         Date     From Customer Address Requirment     Contact
10 2020-03-22     Arry        B      上海      2层阳光板  1864567343
13 2020-03-25     Arry        B      上海      4层阳关板  1864567346
18 2020-03-30     Arry        B      上海        阳光板  1874567342
24 2020-04-05     Arry        B      上海        阳光板  1834567341
10 2020-03-22      Bob        B      上海      2层阳光板  1864567343
13 2020-03-25      Bob        B      上海      4层阳关板  1864567346
18 2020-03-30      Bob        B      上海        阳光板  1874567342
24 2020-04-05      Bob        B      上海        阳光板  1834567341
10 2020-03-22    Cathy        B      上海      2层阳光板  1864567343
13 2020-03-25    Cathy        B      上海      4层阳关板  1864567346
18 2020-03-30    Cathy        B      上海        阳光板  1874567342
24 2020-04-05    Cathy        B      上海        阳光板  1834567341
10 2020-03-22    Diavd        B      上海      2层阳光板  1864567343
13 2020-03-25    Diavd        B      上海      4层阳关板  1864567346
18 2020-03-30    Diavd        B      上海        阳光板  1874567342
24 2020-04-05    Diavd        B      上海        阳光板  1834567341
10 2020-03-22   Jessia        B      上海      2层阳光板  1864567343
13 2020-03-25   Jessia        B      上海      4层阳关板  1864567346
18 2020-03-30   Jessia        B      上海        阳光板  1874567342
24 2020-04-05   Jessia        B      上海        阳光板  1834567341
10 2020-03-22     John        B      上海      2层阳光板  1864567343
13 2020-03-25     John        B      上海      4层阳关板  1864567346
18 2020-03-30     John        B      上海        阳光板  1874567342
24 2020-04-05     John        B      上海        阳光板  1834567341
10 2020-03-22     Lily        B      上海      2层阳光板  1864567343
13 2020-03-25     Lily        B      上海      4层阳关板  1864567346
18 2020-03-30     Lily        B      上海        阳光板  1874567342
24 2020-04-05     Lily        B      上海        阳光板  1834567341
10 2020-03-22  Michcal        B      上海      2层阳光板  1864567343
13 2020-03-25  Michcal        B      上海      4层阳关板  1864567346
18 2020-03-30  Michcal        B      上海        阳光板  1874567342
24 2020-04-05  Michcal        B      上海        阳光板  1834567341
10 2020-03-22   Oliver        B      上海      2层阳光板  1864567343
13 2020-03-25   Oliver        B      上海      4层阳关板  1864567346
18 2020-03-30   Oliver        B      上海        阳光板  1874567342
24 2020-04-05   Oliver        B      上海        阳光板  1834567341
10 2020-03-22  Rebbita        B      上海      2层阳光板  1864567343
13 2020-03-25  Rebbita        B      上海      4层阳关板  1864567346
18 2020-03-30  Rebbita        B      上海        阳光板  1874567342
24 2020-04-05  Rebbita        B      上海        阳光板  1834567341
10 2020-03-22   Sabria        B      上海      2层阳光板  1864567343
13 2020-03-25   Sabria        B      上海      4层阳关板  1864567346
18 2020-03-30   Sabria        B      上海        阳光板  1874567342
24 2020-04-05   Sabria        B      上海        阳光板  1834567341
本次任务用时0.867s
输出文件位置:
C:/Users/Administrator/Desktop/Python/write/5/客户一致性筛查.xlsx

Process finished with exit code 0

在这里插入图片描述
在这里插入图片描述
成功,Well Done! 只要0.867s!没有什么是比时间还珍贵。
But,还有一句中文没有说哦!好吧,太熟悉了,

人生苦短,我用Python!

### 回答1: 以下是可以用 Python 遍历文件夹下所有 Excel 并根据“代码”这一列将同一行的数字保存到的 Excel 的代码: ```python import os import pandas as pd # 定义要遍历文件夹路径 folder_path = 'your_folder_path' # 定义要保存的 Excel 文件路径 new_excel_path = 'your_new_excel_path' # 定义要遍历文件后缀名 file_extension = '.xlsx' # 定义要筛选的列名 column_name = '代码' # 定义要保存的列名 save_column_name = '数字' # 获取文件夹下所有符合条件文件路径 file_paths = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.endswith(file_extension)] # 遍历所有文件读取数据 data = [] for file_path in file_paths: df = pd.read_excel(file_path) data.append(df) # 合并所有数据 merged_data = pd.concat(data) # 筛选出符合条件的行并保存到 Excel 文件 new_data = merged_data.loc[:, [column_name, save_column_name]] new_data.to_excel(new_excel_path, index=False) ``` 注意:在运行代码前需要先安装 pandas 库。 ### 回答2: 可以使用Pythonpandas库和os库来实现遍历文件夹下所有Excel文件,并根据指定列的值将同一行的数字保存到的Excel文件中。具体代码如下: ```python import os import pandas as pd # 定义文件夹路径 folder_path = "文件夹路径" # 定义保存结果的Excel文件路径 result_file = "保存结果的文件路径.xlsx" # 遍历文件夹下的所有文件 files = os.listdir(folder_path) # 初始化一个空的DataFrame用来保存结果 result_df = pd.DataFrame() # 遍历每个文件 for file in files: file_path = os.path.join(folder_path, file) # 判断文件是否为Excel文件 if file_path.endswith(".xlsx") or file_path.endswith(".xls"): # 读取Excel文件的内容 df = pd.read_excel(file_path) # 根据‘代码’这一列将同一行的数字保存到结果中 result_df = pd.concat([result_df, df['代码']], axis=1) # 仅保存‘代码’所在的列 # 将结果保存到的Excel文件中 result_df.to_excel(result_file, index=False) ``` 请将"文件夹路径"替换为要遍历文件夹路径,将"保存结果的文件路径.xlsx"替换为保存结果的Excel文件路径。 该代码可以遍历指定文件夹下的所有Excel文件,将每个Excel文件指定列的值保存到结果中,并将结果保存到的Excel文件中。 ### 回答3: 可以使用Python中的os和pandas库来实现遍历文件夹下所有的Excel文件,并根据某一列的值保存同一行的数字到的Excel文件。 首先需要导入所需的库: ```python import os import pandas as pd ``` 然后定义一个函数来遍历文件夹下的所有Excel文件并保存所有数字到的Excel文件中: ```python def find_excel_files(folder_path): # 创建一个空的DataFrame,用于保存所有数字 all_numbers = pd.DataFrame() # 遍历文件夹下的所有文件和子文件夹 for root, dirs, files in os.walk(folder_path): for file in files: # 检查文件的扩展名是否为Excel文件 if file.endswith(".xlsx") or file.endswith(".xls"): file_path = os.path.join(root, file) # 读取Excel文件的内容 excel_data = pd.read_excel(file_path) # 提取‘代码’这一列的值,并保存到的DataFrame中 numbers = excel_data['代码'].values all_numbers = all_numbers.append(pd.DataFrame(numbers), ignore_index=True) # 将保存的所有数字保存到的Excel文件中 all_numbers.to_excel("输出文件.xlsx", index=False) ``` 最后,调用这个函数并传入文件夹的路径来执行遍历和保存操作: ```python folder_path = "/文件夹路径" find_excel_files(folder_path) ``` 其中,`/文件夹路径`需要替换成实际的文件夹路径。 执行完代码后,会在当前目录下生成一个名为"输出文件.xlsx"的Excel文件,其中包含了所有Excel文件中‘代码’这一列的数字。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值