Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.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从多个excel文件创建多个数据框的步骤_Python_Python 3.x_Pandas_Dataframe - Fatal编程技术网

Python 使用pandas从多个excel文件创建多个数据框的步骤

Python 使用pandas从多个excel文件创建多个数据框的步骤,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我在一个文件夹中有许多excel文件。 任务是使用循环将这些excel文件作为单个数据帧读取,并根据用户插入的键合并它们。我能够在一个列表中得到文件名。 如何迭代列表以将excel文件作为单个数据帧加载 import pandas as pd import os os.chdir(r"C:\Users\user\Desktop\RPA") file_list= [] for root, dirs, files in os.walk(r"C:\Users\use

我在一个文件夹中有许多excel文件。 任务是使用循环将这些excel文件作为单个数据帧读取,并根据用户插入的键合并它们。我能够在一个列表中得到文件名。 如何迭代列表以将excel文件作为单个数据帧加载

import pandas as pd
import os

os.chdir(r"C:\Users\user\Desktop\RPA")

file_list= []

for root, dirs, files in os.walk(r"C:\Users\user\Desktop\RPA"):
    for file in files:
        if file.endswith('.xlsx'):
            file_list.append(file)
print(file_list)

使用
glob
模块进行简单的递归遍历和解析-

import glob
import pd
files = glob.glob('C:\\Users\\user\\Desktop\\RPA\\**\\*.xlsx',recursive = True)
excel_dfs = []
for f in files:
  df = pd.read_excel(f)
  excel_dfs.append(df)

获得文件名列表后,您可以执行以下操作:

df = pd.DataFrame()

for file in file_list:
  this_df = pd.read_excel(file)
  if len(this_df) == 0:
    df = this_df
  else:
    df = pd.merge(left=df, right=this_df, how="inner", left_on="key", right_on="key")

您所说的键是什么意思?您能给我们举个例子吗?两个数据帧中都存在一个列名,用于连接pandasi中的数据帧。我希望将.xlsx文件存储为df1、df2等。因此它可以传递给函数。您可以迭代“文件”列表,并使用pandas.read_excel()将每个文件捕获为dataframe.c=pd.dataframe()对于df_dict:c=pd.read_excel(file)print(c)“”中的文件,我可以打印存储的文件。存储的数据帧的名称是什么?因此,我可以将其作为参数传递给functioni,以获取键上的错误。KeyError:“管理器”。您需要根据用户选择的键更改
左侧的
右侧的
import pandas as pd
import glob
path = r'C:\Users\user\Desktop\RPA'
d=[]
d = glob.glob(path + "/*.xlsx")
print(d)
df= []
for file in d:
    c=pd.read_excel(file)
    df.append(c)
df1 = pd.merge(*df, how="inner", on="Manager")
print(df1)