Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas- ValueError: Usecols do not match columns, columns expected but not found

I am trying to copy some of the columns from the imported csv file to selected.csv but it gives me this error :

'ValueError: Usecols do not match columns, columns expected but not found: ['Status']'; 

It doesn't matter which column name I use it still won't work. I tried printing the headers and it shows them normally, I even tried copying the column names from there so if there is maybe a whitespace I missed or something, but it still gives me the same error. I searched for the answer already but none of the ones I found were right for me.

import pandas as pd
import numpy as numpy
import csv as csv

path_to_import ='C:/Users/Amila/hello/Auftraege_ALSO_R00.csv'

import_file = pd.read_csv(path_to_import, sep=';',engine='python',encoding='utf-8-sig')

headers = pd.read_csv(path_to_import, index_col=0, nrows=0).columns.tolist()

columns = ['Status']

path_to_selected = 'C:/Users/Amila/hello/selected.csv'

pd.read_csv(path_to_import,usecols=columns).to_csv('selected.csv', index=False)

These are the printed column names:

['Auftragsdatum;"Auftrags-Nr.";"Ihre Referenz";"Auftragswert";"Auftragsstatus";"Lieferadresse";"Pos.";"Menge";"Art.Nr.";"Herst.Nr.";"Produktname";"Ihre Referenz (Position)";"Netto / Stk.";"Rechn.-Nr.";"Liefers.-Nr.";"Serien-Nr.";"Status";"Hersteller"']
like image 537
miwa_p Avatar asked Apr 04 '19 10:04

miwa_p


1 Answers

You have inconsistencies in your code:

pd.read_csv(path_to_import,usecols=columns).to_csv('selected.csv', index=False)

you didn't pass the same sep arg, it should be

pd.read_csv(path_to_import,usecols=columns, sep=';').to_csv('selected.csv', index=False)

additionally in your headers line:

headers = pd.read_csv(path_to_import, index_col=0, nrows=0).columns.tolist()

you've passed index_col=0 this treats the first column as an index column which is inconsistent with your other lines so remove it:

headers = pd.read_csv(path_to_import, nrows=0).columns.tolist()
like image 130
EdChum Avatar answered Nov 14 '22 22:11

EdChum