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

28,618

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()
Share:
28,618
miwa_p
Author by

miwa_p

Updated on June 07, 2021

Comments

  • miwa_p
    miwa_p almost 3 years

    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"']