damuzexlxl
damuzexlxl

Reputation: 13

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

I was studying ThinkBayes2 and the example it gives contains code that looks like this: selected = democrat[liberal] all three variables are pandas Series contain boolean type. I've never seen any operator or used it before, So was wondering if anyone can show me how it works here...

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

And the outcome:

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

Couldn't figure out what's going on here...

Upvotes: 1

Views: 107

Answers (1)

DeepSpace
DeepSpace

Reputation: 81614

There is nothing "conditional" or "boolean" going on here, just normal pandas-fashion indexing .

Since b is conceptually [1, 2, 0], a[b] is going to retrieve the content of a but using b as indexes, and in that order.

In other words, a[b] is going to retrieve a[1], a[2], a[0], and is equivalent to a[[1, 2, 0]].

Maybe it will be easier to spot if a contains letters:

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

outputs

1    b
2    c
0    a

And one step simpler, the non-pandas equivalent is

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

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

Upvotes: 2

Related Questions