Hi @genesix32,
for brand in common_brands:
The common_brands
variable contains a list (actually an index object) of the following values:
['volkswagen', 'bmw', 'opel', 'mercedes_benz', 'audi', 'ford']
On each iteration, the iterator (for loop variable), that is brand
will store one of the above values.
Suppose, we are in the first iteration, then this line:
brand_only = autos[autos["brand"] == brand]
will become:
brand_only = autos[autos["brand"] == 'volkswagen']
In the above line, we are filtering the rows based on whether the brand is equal to volkswagen
or not. And we assign the resulting set of volkswagen
rows to brand_only
variable. The next step is to find the mean price of all those volkswagen
cars.
mean_price = brand_only["price"].mean()
Since the brand_only
variable only contains volkswagen
cars, we can just call, the .mean()
method on the price
column.
brand_mean_prices[brand] = int(mean_price)
in the first iteration:
brand_mean_prices['volkswagen'] = int(5402.410261610221)
And the last step in each iteration is to create a key in the brand_mean_prices
dictionary using the brand
variable and assign the value in the mean_price
variable (after converting it to an integer) to the dictionary key.
Thus, after the first iteration ends, brand_mean_prices
dictionary, which was initially an empty dictionary, will become this:
{'volkswagen': 5402}
and once the for loop finishs, it will become something like this:
{'volkswagen': 5402,
'bmw': 8332,
'opel': 2975,
'mercedes_benz': 8628,
'audi': 9336,
'ford': 3749}
I hope this has helped you to understand the code. If you have any questions on it, feel free to let me know. I would be happy to help you.
Best,
Sahil