python - 从标签为元组的 pandas 系列中进行选择

标签 python pandas

我正在尝试执行以下操作:

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)]

其中pdnp分别是pandasnumpy。然而,这会导致

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/

相关文章:

Python argparse 可选破折号整数参数

python - Pandas ,将多列的多个功能应用于groupby对象

python - pandas 返回列名 对每一行应用函数

python - Pandas 数据框 : get rows with same pair of values in two specific columns

python - 两列中的 pd.groupby sum() 函数不起作用

没有 main 的 Python unittest 模块命令行参数

python - PySpark:根据不同列中的一个值的最后一次出现来填充列

python - sqlalchemy session 执行搞乱了有效的 postgres 语法

python - 类型错误 : expected string or buffer python re. 搜索

python - 从另一列的子字符串创建列