What does mode(x).mode[0] mean?


from scipy.stats import mode
#Determing the mode for each
outlet_size_mode = data.pivot_table(values=‘Outlet_Size’, columns=‘Outlet_Type’,aggfunc=(lambda x:mode(x.dropna()).mode[0]))

what does the highlighted part mean?
how does the code work?

The output is as provided.



Hi @AnnaList,

Lets understand the code from scratch


This function is used to create a pivot table.

In this function, you are using “Outlet_Type” as column and “Outlet_Size” as values.

Now for aggregation of the values, you are using mode of the “Outlet_Size” for each group in pivot. The steps you are using are

  1. drop NA values in the group if there are any
  2. Take the mode of that group of values
  3. Take mode again to get a single output

To check what is going on internally, I would recommend you to break down the code, and apply each step iteratively to understand what occurs under the hood


Thank You @jalFaizy!