Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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
Excel 尝试制作一个程序,从多个工作簿中获取多个电子表格,并将它们与其他工作簿中的等效表格合并_Excel_Python 3.x_Pandas - Fatal编程技术网

Excel 尝试制作一个程序,从多个工作簿中获取多个电子表格,并将它们与其他工作簿中的等效表格合并

Excel 尝试制作一个程序,从多个工作簿中获取多个电子表格,并将它们与其他工作簿中的等效表格合并,excel,python-3.x,pandas,Excel,Python 3.x,Pandas,这是我到目前为止编写的代码,但它似乎只适用于多个工作簿中的一张工作表。我该怎么办?我想这可能会有帮助 我在一个目录中有3个Excel文件,其中的数据如下所示 有些电子表格有多张表格。它们都具有相同的数据形状,即相同的列和数据类型 导入包,我使用的是glob,这只是获取文件列表的个人偏好 import pandas as pd from os import listdir from os.path import isfile , join folder = "c:/sheets" ex

这是我到目前为止编写的代码,但它似乎只适用于多个工作簿中的一张工作表。我该怎么办?

我想这可能会有帮助

我在一个目录中有3个Excel文件,其中的数据如下所示

有些电子表格有多张表格。它们都具有相同的数据形状,即相同的列和数据类型

导入包,我使用的是glob,这只是获取文件列表的个人偏好

import pandas as pd
 from os import listdir

 from os.path import isfile , join

folder = "c:/sheets"

excel_names = [f for f in listdir(folder) if isfile(join(folder, f))]
 print(excel_names)
 excel_files = []
for item in excel_names:
 item = folder + item excel_files.append(item)
 # read them in
excels = [pd.ExcelFile(name) for name in excel_files]
# turn them into dataframes
 frames = [x.parse(x.sheet_names[0], header=None, index_col=None) 
for x in excels]
# 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 them..
 combined = pd.concat(frames) combined.to_excel(folder+"/combined.xlsx", header=False, index=False)
遍历目录,抓取每个文件,抓取电子表格中的所有工作表,并使用它获取每个工作表的数据帧。每个数据帧都会写入列表

import pandas as pd
import glob
然后使用列表concat来创建所有电子表格及其表格的数据框架

ls_df = []

for file in glob.glob('/location/to/file/*'):
    print(file)
    for sheet in pd.ExcelFile(file).sheet_names:
        ls_df.append(pd.read_excel(file,sheet))
最终得到的数据帧如下所示

导出到excel

df = pd.concat(ls_df)
IIUC

只有当每个
工作表的数据框具有相同的形状(按列)时,这才有效,否则您需要根据您的用例修改和调整代码

步骤1:列出xlsx文件。 步骤2:创建ExcelFile对象列表。 步骤3:创建按工作表拆分的键、值对字典。 最后,将工作表合并到dataframe_字典中: 在50个xlsx文件上进行测试,其中两张表的列数和行数相同,但数据不同


看不出它对您的示例案例不起作用

嘿!您是否试图将多个Excel中的多个工作表合并到一个数据框中?您的预期输出是什么?一个连接数据帧的字典,按工作表分割?@好的小马是的,这就是我所说的trying@Datanovice是的,我正在尝试从多个工作簿的多个工作表中收集数据,并将它们合并到一本工作手册(带叉的工作表)嘿@MohamedHassan这些工作表的形状都一样吗?与字段和数据类型类似?存在错误C:/Users/mohamed.h.mohamad/.spyder-py3/temp.py:9:FutureWarning:排序,因为非连接轴未对齐。熊猫的未来版本将默认更改为“不排序”。要接受将来的行为,请传递“sort=False”。要保留当前行为并消除警告,请传递“sort=True”。嘿@MohamedHassan听起来您的电子表格可能没有完全相同的列。这个答案可能有助于错误的语法[f代表路径中的f。('c:/sheets').glob('*.xlsx')]^SyntaxError:无效语法有什么遗漏吗?@Mohamed现在有一个轻微的拼写检查:)这可以阅读它们,但不能在一个工作簿中收集它们是否需要多行或其他什么?您的请求清楚地表明您希望在excel文件的每一页中收集数据帧,上面给你的是什么?你得到了什么样的输出,因为它对我有用。
df.to_excel('test_4.xlsx', index = 0 )
from pathlib import Path
excels = [f for f in Path('c:/sheets').glob('*.xlsx')]
xls = [pd.ExcelFile(f) for f in excels]
d = {}

for file in xls:
    for sheet in file.sheet_names:
        if sheet not in d:
            d[sheet] = [] # creates the key which is unique
        else:
            pass # ensures we don't overwrite 
        df = pd.read_excel(file,sheet_name=sheet)
        d[f'{sheet}'].append(df)
df_dict = {}
for k,v in d.items():
    df_dict[f'{k}'] = pd.concat(v)

print(df_dict['Sheet1'])
      DatA   Vals
0     A  16577
1     B  37287
2     C  32761
0     A  38707
1     B  17301
..  ...    ...
1     B   6292
2     C  49310
0     A   4401
1     B  12170
2     C   8978

[150 rows x 2 columns]