使用此处pandas找到的文档中的示例,以下索引完美运行,结果为:pd.Seriesimport pandas as pdtuples = [(1, 'red'), (1, 'blue'), (2, 'red'), (2, 'blue')]columns = pd.MultiIndex.from_tuples(tuples, names=('number', 'color'))asdf = pd.DataFrame(columns=columns, index=[0, 1])asdf.loc[:, (1, 'red')]但是如果我稍微改变一下代码,去掉一层,同样的索引就不起作用了:import pandas as pdtuples = [(1,), (2,)]columns = pd.MultiIndex.from_tuples(tuples, names=['number'])asdf = pd.DataFrame(columns=columns, index=[0, 1])asdf.loc[:, (1,)]IndexError Traceback (most recent call last)<ipython-input-43-d55399a979fa> in <module>----> 1 asdf.loc[:, (1,)]/opt/conda/lib/python3.8/site-packages/pandas/core/indexing.py in __getitem__(self, key) 1760 except (KeyError, IndexError, AttributeError): 1761 pass-> 1762 return self._getitem_tuple(key) 1763 else: 1764 # we by definition only have the 0th axis/opt/conda/lib/python3.8/site-packages/pandas/core/indexing.py in _getitem_tuple(self, tup) 1270 def _getitem_tuple(self, tup: Tuple): 1271 try:-> 1272 return self._getitem_lowerdim(tup) 1273 except IndexingError: 1274 pass/opt/conda/lib/python3.8/site-packages/pandas/core/indexing.py in _getitem_lowerdim(self, tup) 1371 # we may have a nested tuples indexer here 1372 if self._is_nested_tuple_indexer(tup):-> 1373 return self._getitem_nested_tuple(tup) 1374 1375 # we maybe be using a tuple to represent multiple dimensions hereIndexError: tuple index out of range此外,将其索引为asdf.loc[:, 1]throws a TypeError,更进一步,将其索引为asdf.loc[:, ((1,),)]works ,但结果是 a pd.DataFrame,而不是pd.Series!为什么会这样?非常感谢您!PS:我有兴趣从这些问题中“抽象”我的代码(一个级别与一个级别中的多个级别pd.DataFrame.columns)。在我工作的公司中,有时我们会获得需要多个级别的客户数据,但有时只需要一个级别。
1 回答
慕容708150
TA贡献1831条经验 获得超4个赞
你有更新你的熊猫版本吗?在 中pandas v1.1.0,您可以像以前一样使用一个级别进行索引,切片返回一个pd.Series
import pandas as pd
tuples = [(1,), (2,)]
columns = pd.MultiIndex.from_tuples(tuples, names=['number'])
asdf = pd.DataFrame(columns=columns, index=[0, 1])
asdf.loc[:, (1,)]
输出:
0 NaN
1 NaN
添加回答
举报
0/150
提交
取消