Exploring Data with pandas: Fundamentals [5. Method Chaining]

Dear friends in community,

I have a question about practice below:

  1. Use Series.value_counts() and Series.loc to return the number of companies with a value of 0 in the previous_rank column in the f500 dataframe. Assign the results to zero_previous_rank .

I think the answer format should be


So I write the code like:

zero_previous_rank = f500['previous_rank'].value_counts().loc['0']

But the answer is

zero_previous_rank = f500['previous_rank'].value_counts().loc[0]

I’d like to know why there’s only 0 inside the brakets instead of “0”?
Thank you all and wish you all have a wonderful day!

To get to the bottom of this it is good to know about the types of values you can have in Python. '0' is a string which you can verify by running

type('0') # str

And then by running

type(0) # int

The reason you can’t use '0' with loc is that Python doesn’t implicitly parse the numbers within a string, it just knows that it has a string and so '0' or some other string like 'abcdefg' have all the same properties and can’t be used interchangeably with ints.

loc accesses the index of a dataframe. If you don’t set the index yourself, it will set it to be integers 0,1,2... which I think makes sense, what do you think?

Fun, non-essential sidenote. You can explicitly set the index, for example if you would want to name each index.

df.index = ['some name', 'some other name', etc...]

And then access it by this index as such

df.loc['some name']

Where the index is indeed a string.

1 Like

Hi @hjorturjonasson,

Could I think that Python can’t figure out ‘number’ because Python can’t distinguish the difference between ‘number’ and ‘abcdefg’?
I’m sorry if my word is not precise but I try my best to realize.
Your example is really good, thanks and it helps a lot!

It is not that it can’t. Indeed int('0') will give you a 0 it is that it doesn’t. Strings remain strings, regardless of what they contain, even if it is nothing but numbers (or perhaps 'True'. This is an important distinction for the translation between human readable Python code and something the computer understands. The key here is that the computer only knows that it is a string, not what it can also be.