[英]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.
这就是我到目前为止所拥有的。
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.