简体   繁体   中英

Pandas wont add column to dataframe while iterating through a directory

When ever I run i try to add a column in pandas dataframe by iterating through a directory like this

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)

it does nothing, no error codes or anything, it just does nothing. But if I don't iterate through a directory and replace that script with this

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)

It works just fine. Does anybody know why this is or how to iterate through a directory and add columns to a dataframe?

You have defined the function add_column but you haven't ran it. Add the line add_column() to the end of your first script to execute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM