Puzzle 19 Holding out for a Hero

Pandas Brain Teasers — by Miki Tebeka (27 / 34)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Off with Their NaNs | TOC | It’s a Date! 👉

heros.py

​ ​import​ ​pandas​ ​as​ ​pd​

​ heros = pd.Series([​'Batman'​, ​'Wonder Woman'​, ​'Superman'​])
​ ​if​ heros.str.find(​'Iron Man'​).any():
​ ​print​(​'Wrong universe'​)
​ ​else​:
​ ​print​(​'DC'​)

Guess the Output

IMPORTANT

Try to guess what the output is before moving to the next page.

images/hline.png

This code will print: Wrong universe

images/hline.png

The str.find documentation says the following:

Return the lowest index in the string where substring sub is found within the slice s[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found.

In the Puzzle 1, Rectified puzzle, we saw that, except for zeros, all numbers’ Boolean values are True.

When you run

​ In [1]: heros.str.find('Iron Man')
​ Out[1]:
​ 0 -1
​ 1 -1
​ 2 -1
​ dtype: int64

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.