我有
data_combined = pd.read_csv("/path/to/creole_data/data_combined.csv", sep=";", encoding='cp1252')
因此,當我嘗試訪問這些行時:
data_combined = data_combined[(data_combined["wals_code"]=="abk") &(data_combined["wals_code"]=="aco")]
我得到一個 KeyError 'wals_code'。然后我檢查了我的列名串列
print(data_combined.columns.tolist())
并在串列中看到 col 名稱“wals_code”。這是列印出來的前幾項。
[',"wals_code","Order of subject, object and verb","Order of genitive and noun","Order of adjective and noun","Order of adposition and NP","Order of demonstrative and noun","Order of numeral and noun","Order of RC and noun","Order of degree word and adjective"]
有人知道我的檔案有什么問題嗎?
uj5u.com熱心網友回復:
問題是您在讀取 ??CSV 檔案時使用的分隔符。使用sep=';',您指示read_csv使用分號 ( ;) 作為列(單元格和列標題)的分隔符,但從您的列列印出來的情況下,您的 CSV 檔案實際上使用逗號 ( ,)。
如果你仔細看,你會發現你的列列印出來的實際上是一個包含一個長字串的串列,而不是代表列名的單個字串的串列。
因此,使用sep=','代替sep=';'(或完全省略它,因為它,是 的默認值sep):
data_combined = pd.read_csv("/path/to/creole_data/data_combined.csv", encoding='cp1252')
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/429527.html
標籤:python-3.x 熊猫 数据框 CSV
下一篇:將csv轉換為特定的json格式
