以下是我CSV file的名字runs.csv:
PLAYER NAME, TEST 1, TEST 2, TEST 3, TEST 4, TEST 5
Sachin Tendulkar, 167, 134, 108, 100, 89
Rohit Sharma, 147, 78, 101, 36, 23
Mayank Aggarwal, 230, 143, 67, 90, 21
Virendar Sehwag, 75, 44, 12, 8, 98
M.S. Dhoni, 176, 234, 106, 86, 33
Yuvraj Singh, 445, 239, 123, 215, 67
KL Rahul, 290, 128, 76, 111, 336
Kapil Dev, 104, 87, 65, 90, 200
Sunil Gavaskar, 202, 103, 65, 21, 460
K. Srikanth, 222, 110, 97, 34, 02
Mahendar Amarnath, 12, 43, 87, 267, 341
Ajinkya Rahane, 123, 38, 01, 17, 66
以下是我用 jupyter notebook 撰寫的程式:
import pandas as pd
cols = ["PLAYER NAME", "TEST 4"]
runs = pd.read_csv("runs.csv")
print(runs[cols])
我收到以下錯誤:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-28-1eb63cfedd09> in <module>
2 cols = ["PLAYER NAME", "TEST 4"]
3 runs = pd.read_csv("runs.csv")
----> 4 print(runs[cols])
~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
2906 if is_iterator(key):
2907 key = list(key)
-> 2908 indexer = self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1]
2909
2910 # take() does not accept boolean indexers
~\anaconda3\lib\site-packages\pandas\core\indexing.py in _get_listlike_indexer(self, key, axis, raise_missing)
1252 keyarr, indexer, new_indexer = ax._reindex_non_unique(keyarr)
1253
-> 1254 self._validate_read_indexer(keyarr, indexer, axis, raise_missing=raise_missing)
1255 return keyarr, indexer
1256
~\anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
1302 if raise_missing:
1303 not_found = list(set(key) - set(ax))
-> 1304 raise KeyError(f"{not_found} not in index")
1305
1306 # we skip the warning on Categorical
KeyError: "['TEST 4'] not in index"
我不知道我的代碼有什么問題。我認為它寫得正確,但我仍然遇到錯誤。請解決這個問題。提前致謝。
uj5u.com熱心網友回復:
如果您檢查列名稱:
runs.columns
您會注意到“TEST”列名稱前面有一個空格。
Index(['PLAYER NAME', ' TEST 1', ' TEST 2', ' TEST 3', ' TEST 4', ' TEST 5'], dtype='object')
您需要通過應用 strip() 字串方法清除空白區域:
runs.columns = runs.columns.str.strip()
或將您的 cols 變數更改為:
cols = ["PLAYER NAME", " TEST 4"]
以匹配列名。
uj5u.com熱心網友回復:
你可以這樣做:
runs = pd.read_csv("runs.csv", sep=",\s*", engine="python")
或這個:
runs.columns = runs.columns.str.strip()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/392637.html
