简体   繁体   English

Python/pandas For 循环与 Excel,将多个工作簿(单列)合并到搜索列表

[英]Python/pandas For Loop with Excel, Merge multiple Workbooks (Single Column) to Search List

I can get this working on Excel VBA but not python...我可以在 Excel VBA 上运行它,但不能在 python 上运行...

Appreciate if someone can help!感谢有人可以提供帮助! This is what I have so far.这就是我到目前为止所拥有的。

Column name "Search" is the common index which I'm merging off.列名“搜索”是我要合并的公共索引。

import pandas as pd
import os

l = []
for root, dirs, files in os.walk(r"D:/"):
    for file in files:
        if file.endswith(".xlsx"):
             l.append(os.path.join(root, file))


search = 'Search List.xlsx'
source = pd.read_excel(open(search,'rb'), sheet_name=0)
source.set_index("Search", inplace = True)


for i in range(0, len(l)):
    path = l[i]
    df = pd.read_excel(open(path,'rb'), sheet_name=0)
    df.rename(columns={ df.columns[3]: "Search" }, inplace = True)
    df.set_index("Search",inplace = True)

final = pd.merge(source, df, on = ['Search'], how = 'left')

Os.walk gives me a the path of the files ending with xlsx, and creates a list? Os.walk 给了我一个以 xlsx 结尾的文件的路径,并创建一个列表?


['D:/Search\\Find List 1.xlsx', 'D:/Search\\Find List 2.xlsx', 'D:/Search\\Find List 3.xlsx', 'D:/Search\\Find List 4.xlsx'] ['D:/Search\\Find List 1.xlsx', 'D:/Search\\Find List 2.xlsx', 'D:/Search\\Find List 3.xlsx', 'D:/Search\\Find List 4. xlsx']


Once I have the path list, I need to open one at a time, Merge with the "Source" List matching the Column Search.获得路径列表后,我需要一次打开一个,与与列搜索匹配的“源”列表合并。 One by one, i need merge the remaining excel file.一个一个,我需要合并剩余的excel文件。 Does this make sense?这有意义吗?

How do I for loop read excel, merge the Columns that match and then move onto the next iteration of the list.如何循环读取 excel,合并匹配的列,然后移动到列表的下一次迭代。

I am so damn confused我他妈的很困惑

Thank you for your help!感谢您的帮助!

Found the solution after Sammy's suggestion.在 Sammy 的建议下找到了解决方案。 I concat all the Excel files in the list, then I adjusted the data as neccessary before merging with the original search list.我连接列表中的所有 Excel 文件,然后在与原始搜索列表合并之前根据需要调整数据。

import pandas as pd
import os

l = []


for root, dirs, files in os.walk(r"D:/Search"):
    for file in files:
        if file.endswith(".xlsx"):

                df = pd.read_excel(open(file,'rb'), sheet_name=0, header = 0)
                df.rename(columns={ df.columns[3]: "Search" }, inplace = True)
                df["Path"] = file
                l.append(df)

frame = pd.concat(l, axis=0, ignore_index=True)
frame = frame.drop([frame.columns[0] , frame.columns[1], frame.columns[2], frame.columns[4]],  axis='columns')
frame.set_index("Search",inplace = True)


search = 'Search List.xlsx'
source = pd.read_excel(open(search,'rb'), sheet_name=0)
source.set_index("Search", inplace = True)



final = pd.merge(source, frame, on = ['Search'], how = 'left')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将多个Excel工作簿中的多个工作表合并到一个Pandas数据框中 - Merge multiple sheets from multiple Excel workbooks into a single Pandas dataframe 如何使用 Python 将多个 Excel 工作簿合并为一个工作簿 - How to Merge Multiple Excel Workbooks into a single Workbook using Python Excel 和 Python:合并 + Append 具有多个工作表的多个工作簿 - Excel and Python: Merge + Append multiple workbooks with multiple worksheets 将多个工作簿中的单个Excel工作表捕获到熊猫数据框中,并将其保存 - Grabbing a single Excel worksheet from multiple workbooks into a pandas dataframe and saving this 将多个列值合并为一列作为 python pandas 中的列表 - Merge multiple column values into one column as list in python pandas Pandas - 按列值将数据框拆分为多个 Excel 工作簿 - Pandas - Splitting dataframe into multiple excel workbooks by column value 使用Python 3将多个Excel工作簿和工作表导入到单个数据框中 - Using Python 3 to import multiple excel workbooks and sheets into single data frame 熊猫将具有多个值的行数据合并到列的Python列表中 - Pandas Merge row data with multiple values to Python list for a column Python Pandas 将 3 列列表合并为一列 - Python Pandas merge 3 columns of lists in to a single column 在python中使用多个excel工作簿 - Working with multiple excel workbooks in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM