Reputation: 11
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
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
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