简体   繁体   English

set_index()之后的Pandas KeyError

[英]Pandas KeyError after set_index()

I run into a problem while executing this code snippet (Python 3.6.5): 我在执行此代码片段时遇到了问题(Python 3.6.5):

dataset = pd.read_csv('C:/dataset/2014_california_eq_metadata.csv', header=0)
dataset = dataset.set_index("TweetID")
print(dataset["TweetID"])

The error I get is the following one, and it is returned due to the second line of code, since if I remove that, everything works fine. 我得到的错误是以下一个,并且由于第二行代码而返回,因为如果删除它,一切正常。

Traceback (most recent call last):
  File "feature_extraction.py", line 14, in <module>
    print(dataset["TweetID"])
  File "C:\Python36\lib\site-packages\pandas\core\frame.py", line 2139, in __getitem__
    return self._getitem_column(key)
  File "C:\Python36\lib\site-packages\pandas\core\frame.py", line 2146, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Python36\lib\site-packages\pandas\core\generic.py", line 1842, in _get_item_cache
    values = self._data.get(item)
  File "C:\Python36\lib\site-packages\pandas\core\internals.py", line 3843, in get
    loc = self.items.get_loc(item)
  File "C:\Python36\lib\site-packages\pandas\core\indexes\base.py", line 2527, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\_libs\index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'TweetID'

So, my question is: why can't I access a dataframe column using the syntax: 所以,我的问题是:为什么我不能使用以下语法访问数据框列:

dataframe[col_name]

if the specified column name is the dataframe's index? 如果指定的列名是数据帧的索引?

Is there another way to get the Pandas Series corresponding to the index column? 是否有另一种方法可以使Pandas系列对应于索引列?

Yes, way is call .index : 是的,方式是打电话.index

dataset = pd.DataFrame({'TweetID':list('abcdef'),
                       'B':[4,5,4,5,5,4],
                        'C':[7,8,9,4,2,3]})

print (dataset)
   B  C TweetID
0  4  7       a
1  5  8       b
2  4  9       c
3  5  4       d
4  5  2       e
5  4  3       f

dataset = dataset.set_index("TweetID")

print(dataset.index)
Index(['a', 'b', 'c', 'd', 'e', 'f'], dtype='object', name='TweetID')

For Series are 2 ways - Index.to_series of Series constructor (if not specify with default rangeindex): 对于Series有两种方法 - Series构造函数的Index.to_series (如果没有指定默认的rangeindex):

print(dataset.index.to_series())
TweetID
a    a
b    b
c    c
d    d
e    e
f    f
Name: TweetID, dtype: object

print(pd.Series(dataset.index))
0    a
1    b
2    c
3    d
4    e
5    f
Name: TweetID, dtype: object

If MultiIndex then is possible specify level by name: 如果可以使用MultiIndex则可以按名称指定级别:

dataset = dataset.set_index(["TweetID", 'B'])
print(dataset)
           C
TweetID B   
a       4  7
b       5  8
c       4  9
d       5  4
e       5  2
f       4  3

print(dataset.index.get_level_values('TweetID'))
Index(['a', 'b', 'c', 'd', 'e', 'f'], dtype='object', name='TweetID')

or by positions: 或者按职位:

print(dataset.index.get_level_values(0))
Index(['a', 'b', 'c', 'd', 'e', 'f'], dtype='object', name='TweetID')

(it working with single index too, but there dataset.index in enough) (它也使用单个索引,但有足够的dataset.index

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM