pandas中括号运算符在计算条件概率时是如何工作的?

How does the bracket operator work in pandas when calculating conditional probabilities?

我正在研究 ThinkBayes2,它给出的示例包含如下代码: selected = democrat[liberal] 所有三个变量都是 pandas 系列包含布尔类型。我以前从未见过或使用过任何运算符,所以想知道是否有人可以在这里向我展示它是如何工作的...

a = pd.Series([1, 0, 1])
b = pd.Series([1, 2, 0])
a, b, a[b], b[a]

结果:

(0    1
 1    0
 2    1
 dtype: int64,
 0    1
 1    2
 2    0
 dtype: int64,
 1    0
 2    1
 0    1
 dtype: int64,
 1    2
 0    1
 1    2
 dtype: int64)

无法弄清楚这里发生了什么...

这里没有任何“条件”或“布尔”,只是正常的 pandas-时尚索引。

由于 b 在概念上是 [1, 2, 0]a[b] 将检索 a 的内容,但使用 b 作为索引,并按此顺序.

换句话说,a[b]将检索a[1], a[2], a[0],相当于a[[1, 2, 0]]

如果 a 包含字母,也许会更容易发现:

a = pd.Series(['a', 'b', 'c'])
b = pd.Series([1, 2, 0])
print(a[b])

产出

1    b
2    c
0    a

更简单的一步,非pandas等价物是

a = ['a', 'b', 'c']
b = [1, 2, 0]
print([a[index] for index in b])

['b', 'c', 'a']