我正在尝试执行以下操作:
In [11]: import itertools
In [12]: states = itertools.product(range(2), range(3))
In [13]: s = pd.Series(np.zeros(6), index=states)
In [14]: s.at[(0, 0)]
其中pd
和np
分别是pandas
和numpy
。然而,这会导致
ValueError: At based indexing on an non-integer index can only have non-integer indexers
我无法从 http://pandas.pydata.org/pandas-docs/version/0.15.0/indexing.html 中找出答案这有什么问题吗?据我了解,选择单个值时 .at
相当于 .loc
,这里我传递元组 (0, 0)
作为索引。我怎样才能让它发挥作用? (我更喜欢使用 pandas 的优化方法 at
/loc
而不是 __getitem__
方法)。
最佳答案
在我看来,这里最好使用 MultiIndex
并按 loc
选择:
import itertools
states = list(itertools.product(range(2), range(3)))
s = pd.Series(range(6), index=pd.MultiIndex.from_tuples(states))
print (s)
0 0 0
1 1
2 2
1 0 3
1 4
2 5
dtype: int32
print (s.loc[(0, 0)])
0
编辑:
以及使用 MultiIndex.from_product
进行 OP 通信的更好解决方案:
s = pd.Series(range(6), index=pd.MultiIndex.from_product([range(2), range(3)]))
print (s)
0 0 0
1 1
2 2
1 0 3
1 4
2 5
dtype: int32
关于python - 从标签为元组的 pandas 系列中进行选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46973666/