In 7/9 stats Module of "mode. The output of mode : 0 6. Here, is that true? 0 refers to index and 6 is the mode value?
my original code " houses[‘Mo Sold’].mode() " did not work.
The error says ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Only I changed the code as the solution’s— mode(), then it works.
I did notice in our FAQ discussion forum, someone asked the same question and whoever gave the answer didn’t clarify my confusion. Do we always have to have mode()? I checked geeksforgeeks or other documentation, it didn’t suggest mode().
Return the mode(s) of the dataset.
Always returns Series even if only one value is returned.
dropna : bool, default True
Don't consider counts of NaN/NaT.
.. versionadded:: 0.24.0
Modes of the Series in sorted order.
x = houses["Mo Sold"].mode()
.mode() returns a series regardless whether one value is returned.
@alvinctk Thank you for your explanation. It does explain this question very well. But here comes a new question.
So the output(in my picture) of "print(mode) is 0 and 6 means what? two modes or 0 refers to index and 6 is mode?
I am confused of this picture you showed me. Is it a series or an array? 0 on the top right means column index and 0 on the bottom left means row indexing?
I did notice in stats course, I have to be careful of differentiating list/array and series because they represent different way to write the function. eg .series.sum() or sum(array).
Pandas follows the numpy convention of raising an error when you try to convert something to a bool. This happens in a if or when using the boolean operations, and, or, or not. It is not clear what the result of.
5 == pd.Series([12,2,5,10])
The result you get is a Series of booleans, equal in size to the pd.Series in the right hand side of the expression. So, you get an error. The problem here is that you are comparing a pandas pd.Series with a value, so you’ll have multiple True and multiple False values, as in the case above. This of course is ambiguous, since the condition is neither True or False. You need to further aggregate the result so that a single boolean value results from the operation. For that you’ll have to use either any or all depending on whether you want at least one (any) or all values to satisfy the condition.