Puzzle 5 Let’s Get Schwifty

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

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Round and Round We Go | TOC | Full of It 👉

sanchez.py

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

​ s = pd.Series([​'Rick'​, ​'Morty'​, ​'Summer'​, ​'Beth'​, ​'Jerry'​])
​ ​print​(s.lower())

Guess the Output

IMPORTANT

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

images/hline.png

This code will raise an AttributeError.

images/hline.png

The pandas.Series has a lot of methods:

​ In [1]: ​import​ ​pandas​ ​as​ ​pd​
​ In [2]: sum(1 ​for​ attr ​in​ dir(pd.Series) ​if​ attr[0] != ​'_'​)
​ Out[2]: 207

But lower is not one of them:

​ In [3]: hasattr(pd.Series, ​'lower'​)
​ Out[3]: False

Most of the time, people use Pandas with numerical data. The Pandas developers decided to move non-numerical methods out of the (already big) pandas.Series top-level API. To make the teaser code work, use the .str attribute:

--

--

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.