无法使用Panda数据帧提取列名


KeyError: 'Name'
>>> df=pd.read_csv(text_file)
>>> print(df)
Name Age
0     Ritesh 32
1  Priyanka 29 
>>> print(df['Name'].where(df['Name'] == 'Ritesh'))
Traceback (most recent call last):
File "/Users/reyansh/venv/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 2646, in get_loc
return self._engine.get_loc(key)
File "pandas/_libs/index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Name'
During handling of the above exception, another exception occurred:

read_csv没有将文件读取为两列,因为您有一个空格作为分隔符,默认值是逗号。指定空间:

df = pd.read_csv(text_file, " ")

最新更新