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

Why we used[0] in the mode code line, but not other lines?

What is [0] in the mode code line, and why other 2 lings we do not need to use [0]? Thank you!!

Screen Link: https://app.dataquest.io/m/307/the-mode/7/symmetrical-distributions

My Code:

import matplotlib.pyplot as plt
plt.axvline(houses['Mo Sold'].mode()[0],color='Green',label='mode')
plt.axvline(houses['Mo Sold'].median(),color='Orange',label='median')
plt.axvline(houses['Mo Sold'].mean(), color = 'Black', label = 'Mean')
plt.legend()
3 Likes

Hi @candiceliu93,

houses['Mo Sold'].mode() returns a Series object (and in general, Series.mode() always returns a Series object. This is because there can be several mode values in a distribution, but only 1 mean and 1 median. Anyway, even though in this case we have only 1 mode, the returned object for mode() is a Series. So, if we need to extract the exact value of our mode, we have to use this [0].

For median() and mean() methods, instead, the returned value is always a float.

6 Likes