英文:
Unexpected Python KeyError python
问题
我已加载了一个CSV文件
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import norm
然后将加载这些数据
data = pd.read_csv('data.csv')
打印数据
print(data.columns)
输出:Index(['age;SEX;bmi;bp;S1;S2;S3;S4;S5;S6;Y'], dtype='object')
获取列 'age'、'bmi' 和 'bp'
age_data = data['age'].values
bmi_data = data['bmi'].values
bp_data = data['bp'].values
但出现错误:
KeyError: 'age'
有人能看出我错在哪吗?当尝试从一个不匹配名称的列中获取数据时,或者如果该列没有该标题名称时,会生成此错误。然而,它是匹配的。
错误信息:
/usr/local/lib/python3.10/dist-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
/usr/local/lib/python3.10/dist-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'age'
上述异常是以下异常的直接原因:
KeyError Traceback (most recent call last)
<ipython-input-12-92eb9b4ecfe3> in <cell line: 2>()
1 # Mendapatkan kolom 'age', 'bmi', dan 'bp'
----> 2 age_data = data['age'].values
3 bmi_data = data['bmi'].values
4 bp_data = data['bp'].values
5 # Menghitung jumlah kemunculan setiap jenis kelamin
/usr/local/lib/python3.10/dist-packages/pandas/core/frame.py in __getitem__(self, key)
3805 if self.columns.nlevels > 1:
3806 return self._getitem_multilevel(key)
-> 3807 indexer = self.columns.get_loc(key)
3808 if is_integer(indexer):
3809 indexer = [indexer]
/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3802 return self._engine.get_loc(casted_key)
3803 except KeyError as err:
-> 3804 raise KeyError(key) from err
3805 except TypeError:
3806 # If we have a listlike key, _check_indexing_error will raise
KeyError: 'age'
英文:
I have loaded a CSV file
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import norm
This data will then be loaded
data = pd.read_csv('data.csv')
Print data
print(data.columns)
output :Index(['age;SEX;bmi;bp;S1;S2;S3;S4;S5;S6;Y'], dtype='object')
Get column 'age', 'bmi', and 'bp'
age_data = data ['age'].values
bmi_data = data ['bmi'].values
bp_data = data ['bp'].values
and the error:
KeyError: 'age'
Can anyone see where I'm going wrong? This error is generated when you try to grab data from a column which doesn't match the name, or if the column doesn't have that header namne. However, it does.
Error Information:
/usr/local/lib/python3.10/dist-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
/usr/local/lib/python3.10/dist-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'age'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
<ipython-input-12-92eb9b4ecfe3> in <cell line: 2>()
1 # Mendapatkan kolom 'age', 'bmi', dan 'bp'
----> 2 age_data = data ['age'].values
3 bmi_data = data ['bmi'].values
4 bp_data = data ['bp'].values
5 # Menghitung jumlah kemunculan setiap jenis kelamin
/usr/local/lib/python3.10/dist-packages/pandas/core/frame.py in __getitem__(self, key)
3805 if self.columns.nlevels > 1:
3806 return self._getitem_multilevel(key)
-> 3807 indexer = self.columns.get_loc(key)
3808 if is_integer(indexer):
3809 indexer = [indexer]
/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3802 return self._engine.get_loc(casted_key)
3803 except KeyError as err:
-> 3804 raise KeyError(key) from err
3805 except TypeError:
3806 # If we have a listlike key, _check_indexing_error will raise
KeyError: 'age'
答案1
得分: 1
你的表格是用分号 (;
) 分隔的,但是你在 read_csv
函数中使用了 sep
参数的默认值,即逗号 (,
)。
尝试: pandas.read_csv(filename, sep=";")
。
英文:
You table is delimited with semicolons (;
), but you used the default value for sep
parameter in read_csv
(a comma ,
)
Try: pandas.read_csv(filename, sep=“;”)
答案2
得分: 0
output: 在打印数据时,如果你看输出,你会得到:
output :Index(['age;SEX;bmi;bp;S1;S2;S3;S4;S5;S6;Y'], dtype='object')
其中索引是整个第一行作为一个块。如果你打印整个数据框:
print(data)
你应该会看到所有内容都在第一列。你需要用分号 ';' 而不是逗号来分隔,正如另一位评论者指出的那样。
英文:
IF you look at the output when you print the data, you get:
output :Index(['age;SEX;bmi;bp;S1;S2;S3;S4;S5;S6;Y'], dtype='object')
where the index is the entire first line as one blob. if you print out the whole dataframe:
print(data)
You should see that everything is in the first column. You'll need to separate by semi-colon ';' instead of comma, as the other commenter noted.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论