0
import csv
import os
import pandas as pd

#This nested for loop iterates through my excel files and then appends them to my list.
feature_classes = []
for dirpath, dirnames, datatypes in arcpy.da.Walk(workspace,
                                                  datatype="Table"):
                                                  
    for datatype in datatypes:
        feature_classes.append(os.path.join(dirpath, filename))

#This loop is intended to delete one column in each excel file. Currently It only deletes the column for the first file.
for fc in feature_classes:
        arcpy.management.DeleteField(os.path.join(dirpath, filename),"Delete_")

This code successfully deletes the field I want, but only from the first Excel file in the directory I am looping for. I think a nested for loop may be required but wanted to see what other simpler solutions may exist.

1
  • dirpath doesn't vary in the loop. You want to use fc.
    – Vince
    Sep 16 at 21:05
1

To do this, there is no need for ArcPy:

import os
import pandas as pd

mainfolder = r'C:\GIS\data\testdata'
drop_column = 'CRY'

for root, folder, files in os.walk(mainfolder): #Will include all subfolders. If you dont want this use os.listdir
    for file in files:
        fullname = os.path.join(root, file)
        if os.path.isfile(fullname) and file.endswith('.xlsx'):
            df = pd.read_excel(fullname)
            df = df[[col for col in df.columns if col!=drop_column]] #Select all columns except drop column
            df.to_excel(fullname) #Will overwrite your original file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.