Pedro Talhavini
Pedro Talhavini

Reputation: 11

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

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)

Upvotes: 1

Views: 511

Answers (2)

San
San

Reputation: 21

Before converting the dataframe to excel, You could try to extract the information in the dataframe by excluding the unnamed column. For example, below line of code will extract everything excluding Unnamed attribute tabela = tabela.loc[:, ~tabela.columns.str.contains('^Unnamed')] . Hope this helps!

Upvotes: 1

Nicolai B. Thomsen
Nicolai B. Thomsen

Reputation: 884

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

Upvotes: 3

Related Questions