There is a dataset of vehicles by type (sedan, SUV, truck, etc), odometer, cylinders, price, etc. I am addressing the missing values in the column 'cylinders', which contains the number of cylinders in the engine of the vehicle. My approach to fill in the missing values is to use the median number of cylinders per type of vehicle. Using a pivot table it looks like this: Screenshot of the pivot table
Now I want to create a for loop that goes through every row and when it finds a NaN value in column 'cylinders' replaces it with the median value seen in the pivot table according to the type.
Thanks