FVerg
FVerg

Reputation: 93

Pandas KeyError after set_index()

I run into a problem while executing this code snippet (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?

Upvotes: 7

Views: 3899

Answers (1)

jezrael
jezrael

Reputation: 862591

Yes, way is call .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):

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:

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)

Upvotes: 2

Related Questions