Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pandas-在.xlsx文件的文件夹中循环,仅使用正则表达式从Excel选项卡中添加名称为xx.xx的数据_Python_Excel_Regex_Pandas_Dataframe - Fatal编程技术网

Python Pandas-在.xlsx文件的文件夹中循环,仅使用正则表达式从Excel选项卡中添加名称为xx.xx的数据

Python Pandas-在.xlsx文件的文件夹中循环,仅使用正则表达式从Excel选项卡中添加名称为xx.xx的数据,python,excel,regex,pandas,dataframe,Python,Excel,Regex,Pandas,Dataframe,我有将近30个Excel文件的数据集。我已经能够循环遍历文件夹并将它们全部附加到一个数据集中。我的问题是,这些Excel文件中有一些包含了我需要的多个选项卡的数据。我需要的所有选项卡都具有选项卡名称中表示的相同日期模式(例如01.21)。显然,正则表达式是我需要的,我知道我需要的正则表达式模式,我的问题是我不知道如何使用Pandas循环每个Excel文件,使用正则表达式检查选项卡名称,并且只添加字符串中包含xx.xx的选项卡中的数据。例如,如果我打开一个Excel文件,其中有3个选项卡:“dat

我有将近30个Excel文件的数据集。我已经能够循环遍历文件夹并将它们全部附加到一个数据集中。我的问题是,这些Excel文件中有一些包含了我需要的多个选项卡的数据。我需要的所有选项卡都具有选项卡名称中表示的相同日期模式(例如01.21)。显然,正则表达式是我需要的,我知道我需要的正则表达式模式,我的问题是我不知道如何使用Pandas循环每个Excel文件,使用正则表达式检查选项卡名称,并且只添加字符串中包含xx.xx的选项卡中的数据。例如,如果我打开一个Excel文件,其中有3个选项卡:“data 01.22”、“financials”和“data 03.23”,我只希望它添加“data 01.22”和“data 03.23”中的数据

我需要在这些选项卡中标识名称模式的正则表达式模式是[0-9][0-9]+[0-9][0-9]。我知道我很接近,但我错过了一些关键的东西,任何帮助都是感激的

import pandas as pd
import os
import re

# filenames
files = os.listdir()    
excel_names = list(filter(lambda f: f.endswith('.xlsx'), files))

# read them in
excels = [pd.ExcelFile(name, engine='openpyxl') for name in excel_names]

# turn them into dataframes
frames = [x.parse(x.sheet_names[0], header=None,index_col=None) for x in excels]

#These are the tabs 
sh = [x.sheet_names for x in excels]

# I know I need to use this regex below, but where is the question:

#sheet_match = re.findall("[0-9][0-9]+\.[0-9][0-9]", s)

# delete the first row for all frames except the first
# i.e. remove the header row -- assumes it's the first
frames[1:] = [df[1:] for df in frames[1:]]

# concatenate 
combined = pd.concat(frames)

# export 
combined.to_excel("combinedfiles.xlsx", header=False, index=False)



你真的很接近了,你只需要用
re.match
过滤表名。循环浏览每个Excel文件,对于每个文件,打开它并获取选项卡名称列表(
Excel\u file.sheet\u names
)与已定义的表达式一起使用,以仅获取与所需模式匹配的选项卡。阅读这些工作表的内容(
sheet\u name=valid\u sheets
),根据具体情况调整标题和索引,然后将每个excel文件的提取内容添加到列表中。将列表与
pd.concat
连接起来,并生成新的excel文件

将熊猫作为pd导入
导入操作系统
进口稀土
#文件名
files=os.listdir()
excel_name=list(过滤器(lambda f:f.endswith('.xlsx'),文件))
regex=r'[0-9][0-9]+\[0-9][0-9]'
框架列表=[]
#循环浏览每个Excel文件
对于excel_名称中的名称:
#打开一个excel文件
excel\u file=pd.ExcelFile(名称,engine='openpyxl')
#获取字符串中包含xx.xx的选项卡列表
有效的\u工作表=[excel中选项卡的选项卡\u file.sheet\u名称,如果重新匹配(regex,tab)]
#从选项卡列表中读取内容
d=excel\u file.parse(工作表名称=有效工作表,页眉=0)
#将内容添加到框架列表中
框架列表+=列表(d.值())
组合=局部固结(框架列表)
combined.to_excel(“combined files.xlsx”,header=False,index=False)