Act fast, special offers end soon! Up to $294 is savings when you get Premium today.
Get offer codes

Why convert series to list?


Screen Link:

On this page, a piece of code is already provided in which one column of a dataframe is extracted as a ‘list’. Unless I missed or forgot something, this came as a surprise to me (as something new), since so far extracting a column from a dataframe just resulted in a ‘series’. So I am wondering why here suddenly this is converted to a list. And when I tried it out, actually things still worked when I did not do that conversion.

My Code:

import re

#titles = hn["title"].tolist() # THIS LINE: ORIGINAL CODE BY DATAQUEST
titles = hn["title"] # THIS LINE: MY CODE

python_mentions = 0
pattern = r"[Pp]ython"

for a_title in titles:
    if, a_title):
        python_mentions += 1
print (python_mentions)

What I expected to happen:
So in a way, I expected this to fail… as I left out the conversion to list which was added by Dataquest.

What actually happened:
It still works.

So that makes me wonder: why the conversion to a list?

Kind regards,

1 Like

Yes you are right. I also don’t see any need for that.

1 Like

Thank you for confirming. Was wondering whether I missed something.

It’s specifically mentioned on the instructions that titles will be extracted as a list. So, my idea is that probably it is done to emphasise that all the work will be done in pure Python…

1 Like