我正在嘗試從 csv 檔案中提取一些行,但無法獲得所需的行。
到目前為止,這是我的代碼:
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)
這是我試圖“清理”的 csv 的一部分:https ://pastebin.com/hUfUYwf0
我設法列印了“作者”(作為獨立的,標題和 isbn 被評論),但是當我嘗試列印“標題”和“isbn”時,我收到了這個錯誤:
回溯(最后一次呼叫):檔案“C:\Users\aky547\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py”,第 3621 行,在 get_loc 回傳self._engine.get_loc(casted_key) 檔案“pandas_libs\index.pyx”,第 136 行,在 pandas._libs.index.IndexEngine.get_loc 檔案“pandas_libs\index.pyx”,第 163 行,在 pandas._libs.index.IndexEngine .get_loc 檔案“pandas_libs\hashtable_class_helper.pxi”,第 5198 行,在 pandas._libs.hashtable.PyObjectHashTable.get_item 檔案“pandas_libs\hashtable_class_helper.pxi”,第 5206 行,在 pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'Autor '
上述例外是以下例外的直接原因:
回溯(最后一次呼叫):檔案“v:\Coding\coding\Python\get data from csv.py”,第 10 行,在 author.append(data['Autor']) 檔案“C:\Users\aky547 \AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\frame.py”,第 3505 行,在getitem indexer = self.columns.get_loc(key) 檔案“C:\Users\aky547\ AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py",第 3623 行,在 get_loc 中從 err KeyError 引發 KeyError(key):'Autor'
我應該怎么做才能獲得所有必需的行,在這種情況下列印(盡管稍后我想將它們保存到新的 csv)?任何幫助表示贊賞。
uj5u.com熱心網友回復:
編輯:如上面的評論,沒有列名為Autor
Verfasser如果您需要作者,您可能需要參加該專欄。
author.append(data['Verfasser'])
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/456661.html
標籤:Python python-3.x 熊猫 CSV
下一篇:比較四個不同列中的每行值
