Closed
Description
The last line of the following code snippet does not work, while it seems it should. Am I overlooking something?
df = pd.DataFrame(np.random.randn(9,2), index=[1,1,1,2,2,2,3,3,3], columns=['a', 'b'])
df
df.loc[[1, 2]] # works
df.loc[:,['a', 'b']] # works
df.loc[[1, 2], ['a', 'b']] # does not work
You don't have this with an index without duplicates.
In [1]: df = pd.DataFrame(np.random.randn(9,2), index=[1,1,1,2,2,2,3,3,3], colum
ns=['a', 'b'])
In [2]: df
Out[2]:
a b
1 0.714735 -1.125141
1 0.061066 -0.737312
1 0.269988 1.434862
2 0.336829 1.040681
2 0.929521 -1.142978
2 0.272813 -0.370184
3 -0.520348 1.126233
3 0.232261 -0.075287
3 -0.314570 -2.635400
[9 rows x 2 columns]
In [3]: df.loc[[1, 2]]
Out[3]:
a b
1 0.714735 -1.125141
1 0.061066 -0.737312
1 0.269988 1.434862
2 0.336829 1.040681
2 0.929521 -1.142978
2 0.272813 -0.370184
[6 rows x 2 columns]
In [4]: df.loc[:,['a', 'b']]
Out[4]:
a b
1 0.714735 -1.125141
1 0.061066 -0.737312
1 0.269988 1.434862
2 0.336829 1.040681
2 0.929521 -1.142978
2 0.272813 -0.370184
3 -0.520348 1.126233
3 0.232261 -0.075287
3 -0.314570 -2.635400
[9 rows x 2 columns]
In [5]: df.loc[[1, 2], ['a', 'b']]
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-5-617013ef905a> in <module>()
----> 1 df.loc[[1, 2], ['a', 'b']]
c:\users\vdbosscj\scipy\pandas-joris\pandas\core\indexing.pyc in __getitem__(sel
f, key)
1152 def __getitem__(self, key):
1153 if type(key) is tuple:
-> 1154 return self._getitem_tuple(key)
1155 else:
1156 return self._getitem_axis(key, axis=0)
c:\users\vdbosscj\scipy\pandas-joris\pandas\core\indexing.pyc in _getitem_tuple(
self, tup)
680 # ugly hack for GH #836
681 if self._multi_take_opportunity(tup):
--> 682 return self._multi_take(tup)
683
684 # no shortcut needed
c:\users\vdbosscj\scipy\pandas-joris\pandas\core\indexing.pyc in _multi_take(sel
f, tup)
726 return o.reindex(**d)
727 except:
--> 728 raise self._exception
729
730 def _convert_for_reindex(self, key, axis=0):
KeyError: