Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/29.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在多个excel中搜索相应的数据并粘贴到新的excel工作表_Python_Excel_Pandas_Openpyxl - Fatal编程技术网

Python在多个excel中搜索相应的数据并粘贴到新的excel工作表

Python在多个excel中搜索相应的数据并粘贴到新的excel工作表,python,excel,pandas,openpyxl,Python,Excel,Pandas,Openpyxl,我在一个文件夹中有一些excel文件,每个文件中都有一个叫做“服务”的工作表 Notes_111.xlsx Notes_222.xlsx Notes_888.xlsx 工作流:我想打开每个.xlsx文件,例如Notes_111.xlsx,然后添加一个名为“code_city”的新工作表,然后根据文件名111,从主数据框中仅提取code=111数据并粘贴到新工作表。然后保存 另一个excel文件中的主数据框示例 code city 0 111

我在一个文件夹中有一些excel文件,每个文件中都有一个叫做“服务”的工作表

Notes_111.xlsx
Notes_222.xlsx
Notes_888.xlsx
工作流:我想打开每个.xlsx文件,例如Notes_111.xlsx,然后添加一个名为“code_city”的新工作表,然后根据文件名111,从主数据框中仅提取code=111数据并粘贴到新工作表。然后保存

另一个excel文件中的主数据框示例

    code           city
0    111            NY
1    111            CA
2    222            NJ
3    888            WE
4    888            TL
我不知道如何在循环中编写逻辑来搜索相应的数据

import pandas as pd
import numpy as np
import glob
from openpyxl import load_workbook

for f in glob.glob(path + "Notes_*.xlsx"):
   wb = load_workbook(f)
   ws = wb.create_sheet('code_city')
   ws['A1'] = 'how to search corresponding data and paste here???'
   wb.save(f)

请帮助。

使用熊猫它更容易操作,我相信它在引擎盖下使用openpyxl

import glob
import pandas as pd
import os


for f in glob.glob('Notes_*.xlsx'):
    dda = re.findall('\d+', f) #matches digits in the filename

    df_each = pd.read_excel(f) # have to save the data first, coz ExcelWriter will clear up and create a new excel, so, you paste the saved data back to new sheet
    df_1_dda = df_master[df_master['code'] == int(dda[0])] #select only those records with code in the filename

    writer = pd.ExcelWriter(f)
    df_each.to_excel(writer, 'service', index = False) #  paste the saved data back to new sheet
    df_1_dda.to_excel(writer, 'code_city', index = False)
    writer.close()
希望有帮助


使用python 3.6.4 Anaconda-32位

文件中的工作表是什么样子?@charlechlark有一个excel文件有一个工作表,类似上面的示例主数据框。工作流程是打开Notes_111.xlsx,添加一个新的工作表,然后根据文件名111,它将只提取主数据框中的code=111,然后将code和city粘贴到新工作表中。请记住,我将主工作表保存为数据框df_master出于某种原因为主数据框更新,使用“code_to_search_by=name.split”(“”“)[1]”这种方式不起作用,我修改了你的代码使其完全起作用,谢谢!!嗯,奇怪,它对我有效…你使用的是什么版本的python?如果我帮了你,请向上投票或作为答案检查:)祝你玩得愉快。我自己的答案有问题,新的df将替换我的旧df数据
from openpyxl import load_workbook

for f in glob.glob("Notes_*.xlsx"):
    code = re.findall('\d+', f) #matches digits in the filename
    df_1_dda = df_master[df_master['code'] == int(code[0])] #select only those records with code from the master dataframe

    #create new worksheet using openpyxl
    wb = load_workbook(f)
    ws = wb.create_sheet('code_city')
    wb.save(f)

    # reload the file and paste data I need
    writer = pd.ExcelWriter(f)
    df_1_dda.to_excel(writer, 'code_city')
    writer.save()