Exploring_Ebay_Car_Sales_Data - common brand/model combinations

Hello everybody, I’m not stuck at any part at the moment but, at the end of the guided project for Pandas and NumPy Fundamentals I found the following extra challenge:

Find the most common brand/model combinations.

And I was challenged enough to find the solution, therefore, I wrote the piece of code below.

# Most common brand/model combinations.
# Importing pandas library.
import pandas as pd
# Opening the dataset.
autos_copy = pd.read_csv('autos.csv',encoding='Latin-1')

autos_copy["brand_model"] = autos_copy.brand + '|' + autos_copy.model
# Had to get rid of this "brand", it is kind of a general brand.
unique_brand = autos_copy.loc[autos_copy["brand"] != 'sonstige_autos',"brand"].unique()
brand_model_list = [] 
for each_brand in unique_brand:
    brand,model = autos_copy.loc[autos_copy.loc[:,"brand"] == each_brand,"brand_model"].value_counts().sort_values(ascending=False).index[0].split("|")
    ads =  autos_copy.loc[autos_copy.loc[:,"brand"] == each_brand,"brand_model"].value_counts().iloc[0]
    #print("Brand:",brand," Model:",model," Ads:",ads)

columns_names = ["brand","model","ads"]
common_brand_model = pd.DataFrame(data=brand_model_list,columns = columns_names)

print("Most common brand/model combinations")

I didn’t get any error, I only want to know if the result is correct, and if the logic is not that wrong, I wasn’t able to come up with a different solution.

1 Like