Guided Project 3 - Car Sales

I am trying to find the most common brand/model combinations in the cars dataset. This is my code. I am struggling to extract the model name from the “model” column. Can you pls give me a hint? thx!

brand_names = autos[“brand”].value_counts().index
brand_model = {}
for x in brand_names:
selected_rows = autos[autos[“brand”] == x]
top_model = selected_rows[“model”].value_counts(ascending = False).head(1)
brand_model = top_model

Hello @m_oblozinsky Welcome to the DataQuest Community!

value_counts returns a series containing counts of unique values. The unique values are the index of the returned series. The resulting object is in descending order.

So to get the most common brand, well need the item and index 0:

top_brand = autos.brand.value_counts().index[0]

The same appears to the most common model:

top_model = autos.model.value_counts().index[0]

thanks! your code works for me outside the loop, however inside the loop I still get an error.

brand_model = {}
for x in brand_names:
selected_rows = autos[autos[“brand”] == x]
top_model = selected_rows[“model”].value_counts().index[0]
brand_model = top_model

IndexErrorTraceback (most recent call last)
in ()
2 for x in brand_names:
3 selected_rows = autos[autos[“brand”] == x]
----> 4 top_model = selected_rows[“model”].value_counts().index[0]
5 brand_model = top_model
6 print(brand_model)

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/indexes/ in getitem(self, key)
1742 if is_scalar(key):
-> 1743 return getitem(key)
1745 if isinstance(key, slice):

IndexError: index 0 is out of bounds for axis 0 with size 0

In simple words can i know what you are trying to achieve?

Sure. I have a database with sold cars (including columns with the “brand” and “model” sold). Now the task is to find the most common brand/model combinations. That means I have to build a dictionary with the key being the brand (for example “volkswagen”) and the other value being the most common model (for example “golf”).

In my code “brand_names” contains the various brands from the database.