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
在数据帧中返回-Python_Python_Excel_Pandas_Conditional_Directory - Fatal编程技术网

在数据帧中返回-Python

在数据帧中返回-Python,python,excel,pandas,conditional,directory,Python,Excel,Pandas,Conditional,Directory,早上好 我有一个关于Python的问题。我有一个if,where有条件,else,它呈现多个文件,我需要将它读取的所有信息保存在一个数据帧中,有没有办法做到这一点 我正在使用的代码: for idx, folder in enumerate(fileLista): if folder == 'filename_for_treatment': df1 = pd.read_excel(folder, sheet_name = sheetName[idx], skiprows=

早上好

我有一个关于Python的问题。我有一个if,where有条件,else,它呈现多个文件,我需要将它读取的所有信息保存在一个数据帧中,有没有办法做到这一点

我正在使用的代码:

for idx, folder in enumerate(fileLista):
    if folder == 'filename_for_treatment':
        df1 = pd.read_excel(folder, sheet_name = sheetName[idx], skiprows=1)
        df1.columns = df1.columns.str.strip()
        tratativaUm = df1[[column information to be used]]

     else:
        df2 = pd.read_excel(folder, sheet_name = sheetName[idx], skiprows=1)
        df2.columns  = df2.columns.str.strip()
        TratativaDois = df2[[column information to be use]]

####assign result of each file received in the else

frames = [tratativaUm, tratativaDois] 
titEmpresa = pd.concat(frames)

有人能帮我吗?有可能吗?谢谢

您可以通过在列表中添加数据帧来完成此操作,例如:

list_df_tratativaDois = []
for idx, folder in enumerate(fileLista):
    df = pd.read_excel(folder, sheet_name = sheetName[idx], skiprows=1)
    df.columns = df.columns.str.strip()
    if folder == 'filename_for_treatment':
        tratativaUm = df[[column information to be used]]
     else:
        list_df_tratativaDois.append(df[[column information to be use]])

titEmpresa = pd.concat([tratativaUm]+list_df_tratativaDois)

请注意,您可以不使用
df1
df2
而是创建一个
df
,因为它是相同的
read\u excel
,然后根据
文件夹是否正确,对
df
执行不同的操作。我尝试了相同的操作,只是他忽略了文件,他只读取了negociadoraA中的一个,这有什么原因吗?@BrendaMoura什么是“negociadoraA”?对不起,negociadoraA=tratativaUm@BrendaMoura如果没有有关
fileLista
的更多信息,我不确定能否回答。您的治疗文件名是唯一的吗?在一天结束时,您希望从多个excel文件中提取一张工作表,并确保满足您条件的工作表是最终DF中的第一张?不是,我有许多文件具有xls扩展名(excel)。我需要读取所有数据并放入一个数据帧中。问题是有一个文件的头不同于其他文件。我输入的第一个代码是这样做的,我的问题是当它到达其他地方时,它覆盖了文件,并且我需要它来考虑所有的信息。