Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.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 熊猫在遍历目录时不会向dataframe添加列_Python_Excel_Dataframe_Loops - Fatal编程技术网

Python 熊猫在遍历目录时不会向dataframe添加列

Python 熊猫在遍历目录时不会向dataframe添加列,python,excel,dataframe,loops,Python,Excel,Dataframe,Loops,每次运行时,我都会尝试通过遍历如下目录在dataframe中添加一列 import pandas as pd import os import sys ########generate file path 'getcwd: ', os.getcwd() osfile, maindir =('__file__: ', __file__) filename = os.path.basename(sys.argv[0]) inpath = maindir.replace(filena

每次运行时,我都会尝试通过遍历如下目录在dataframe中添加一列

import pandas as pd
import os
import sys

########generate file path
'getcwd:      ', os.getcwd()
osfile, maindir =('__file__:    ', __file__)
filename = os.path.basename(sys.argv[0])
inpath = maindir.replace(filename,"Excels")
outpath = maindir.replace(filename,"BulkFile.xlsx")


#########pandas script
def add_column():
    for root, dirs, files in os.walk(inpath):
        print(files)
        for f in files:
            path = os.path.join(root, f)
            excelframe = pd.read_excel(path)
            excelframe['full_name'] = excelframe['first_name'] + " " + excelframe['last_name']
            dataframe = [excelframe]
            compactframe = pd.concat(dataframe)
            compactframe.to_excel(outpath)
它什么也不做,没有错误代码或其他什么,它什么也不做。 但是如果我不遍历一个目录并用这个替换这个脚本

import pandas as pd
import sys
import os



#####generate file path
osfile, maindir =('__file__:    ', __file__)
filename = os.path.basename(sys.argv[0])
inpath = maindir.replace(filename,"BulkFile.xlsx")
outpath = maindir.replace(filename,"newfile.xlsx")




########pandas script
excelframe = pd.read_excel(inpath)
excelframe['full_name'] = excelframe['first_name'] + " " + excelframe['last_name']
dataframe = [excelframe]
compactframe = pd.concat(dataframe)
compactframe.to_excel(outpath)
它很好用。
有人知道为什么会这样,或者知道如何遍历目录并向数据帧添加列吗?

您已经定义了函数
add\u column
,但还没有运行它。在要执行的第一个脚本末尾添加行
Add_column()