Puzzle 2 In or Out?

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

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Rectified | TOC | Month by Month 👉

simpsons.py

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

​ simpsons = pd.Series(
​ [​'Homer'​, ​'Marge'​, ​'Bart'​, ​'Lisa'​, ​'Maggie'​])
​ ​print​(​'Bart'​ ​in​ simpsons)

Guess the Output

IMPORTANT

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

images/hline.png

This code will print: False

images/hline.png

pandas.Series is a sequence type. Most Python sequences are indexed by a range, meaning the first item is at index 0, the second item is at index 1, and so forth.

0 vs. 1

INFORMATION

Python is a 0-based language. Some languages, such as MATLAB, are 1-based. The compromise to use ½ as the first index didn’t go well. :)

pandas.Series (and pandas.DataFrame) indices are more flexible. The default is a range-based index, but there are other types of indices.

​ In [1]: ​import​ ​pandas​ ​as​ ​pd​
​ In [2]…

--

--

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.