Home > Enterprise >  How do I delete "unnamed" columns in my excel document?
How do I delete "unnamed" columns in my excel document?

Time:12-19

I'm trying to edit an excel document but when I use

tabela = pd.read_excel('Tabela de Exercícios.xlsx')

it creates 2 or 3 "unnamed" columns, even when desabiling the index:

tabela.to_excel('Tabela de Exercícios.xlsx', index = False).

Here's the complete code:

import pandas as pd
tabela = pd.read_excel('Tabela de Exercícios.xlsx')
produtos = ['Porca', 'Parafuso', 'Arruela', 'Prego', 'Alicate', 'Martelo']
counter = 0
while counter < len(produtos):
    tabela.loc[tabela['Código'] == counter   1, 'Produto'] = produtos[counter]
    counter  = 1
tabela.to_excel('Tabela de Exercícios.xlsx', index = False)

CodePudding user response:

It is very likely that you have whitespaces in some otherwise empty columns in your spreadsheets. Once you ask pandas to read in the sheet, it will detect these whitespaces as values in those columns and therefore assume that these columns are to be included. Since there's no column names for these columns, pandas will automatically label them as unnamed_n

CodePudding user response:

You could use this code tabela = tabela.loc[:, ~tabela.columns.str.contains('^Unnamed')] and then try to convert it to excel

  • Related