Home > other >  KeyError when trying to print rows from csv
KeyError when trying to print rows from csv

Time:04-06

I am trying to extract some rows from a csv file, but can't get the required rows.

this is my code so far:

import pandas as pd

data=pd.read_csv("V:\Coding\coding\clean.csv", sep="\t")
data.columns
title=[]
author=[]
isbn=[]
title.append(data['Titel'])
author.append(data['Autor'])
isbn.append(data['ISBN'])
print(title)
print(author)
print(isbn)

This is a part of the csv I'm trying to "clean up": https://pastebin.com/hUfUYwf0

I've managed to print the "author" (as standalone, the title and isbn were commented) , but when I try to print the "title" and "isbn" as well, I get this error:

Traceback (most recent call last): File "C:\Users\aky547\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py", line 3621, in get_loc return self._engine.get_loc(casted_key) File "pandas_libs\index.pyx", line 136, in pandas._libs.index.IndexEngine.get_loc File "pandas_libs\index.pyx", line 163, in pandas._libs.index.IndexEngine.get_loc File "pandas_libs\hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas_libs\hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'Autor'

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "v:\Coding\coding\Python\get data from csv.py", line 10, in author.append(data['Autor']) File "C:\Users\aky547\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\frame.py", line 3505, in getitem indexer = self.columns.get_loc(key) File "C:\Users\aky547\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py", line 3623, in get_loc raise KeyError(key) from err KeyError: 'Autor'

What should I do in order to get all required lines to, in this case print (though later on I want to save them to a new csv)? Any help is appreciated.

CodePudding user response:

EDIT: as in the comment above, there is no column named Autor

Probably you need to take the column Verfasser if you need authors.

author.append(data['Verfasser'])
  • Related