Greg
Greg

Reputation: 11

Pandas ValueError: Usecols do not match names

I get a ValueError when I try to read the specific column of a file while using pd.read_csv(). When I read the file without usecols, everything works fine.

I recreated the problem in a simplified manner:

test_column = "Test Column"
data = pd.DataFrame([1, 2, 3, 4, 5], columns=[test_column])

data.to_csv("test_data.csv", decimal=",", sep=";")

data_read = pd.read_csv("test_data.csv", decimal=",", sep=";", usecols=test_column)

Upvotes: 1

Views: 2203

Answers (1)

Bill the Lizard
Bill the Lizard

Reputation: 406125

According to the pandas.read_csv documentation, the usecols parameter is expecting either a list-like object or a callable function. I think the specific error you're getting is because read_csv is interpreting the value you passed to it as a list of characters, none of which match the column names in the file.

So even though you only have one column name, you still need to pass it as a list, similar to when you created the dataframe.

data_read = pd.read_csv("test_data.csv", decimal=",", sep=";", usecols=[test_column])

Upvotes: 1

Related Questions