0

I'm using a dynamic script to remove columns with values equal to column name. Example below for table named summ.

Script:

for col in range(0, len(summ.columns)-1):
     if len(summ[(summ[summ.columns[col]].astype(str).str.lower() == summ.columns[col])]) == len(summ):
          summ = summ.drop(columns = summ.columns[col])

Input: enter image description here

Ideal Output: enter image description here

Output of the script after executing the for loop script above: enter image description here

Once I re-run the script above, it would also delete the lname column but why does the for loop not check all columns?

Janine
  • 37
  • 8
  • 1
    since you are dropping the columns on the fly, the loop counter goes out of range after a point / it will skip columns. instead of doing on the fly, keep track of the column names, and drop them all at once after the loop https://stackoverflow.com/questions/28538536/deleting-multiple-columns-based-on-column-names-in-pandas – Tharun K Dec 25 '21 at 14:10

1 Answers1

0

Fixed it.

colnames = []
    for col in range(0, len(summ.columns)-1):
        if len(summ[(summ[summ.columns[col]].astype(str).str.lower() == summ.columns[col])]) == len(summ):
            colnames.append(summ.columns[col])
    
    for col in range(0,len(colnames)):
        summ = summ.drop(columns = colnames[col]) 
Janine
  • 37
  • 8