简体   繁体   English

Pandas-ValueError:Usecols 与列不匹配,列需要但未找到

[英]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 :我试图将导入的csv文件中的一些列复制到selected.csv但它给了我这个错误:

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

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你没有通过相同的sep arg,应该是

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:您已通过index_col=0这会将第一列视为与您的其他行不一致的索引列,因此将其删除:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM