Untitled

 avatar
unknown
python
a year ago
1.8 kB
12
Indexable
import pandas as pd

# Given the task, I will write the function `topCarModel` which performs operations on a pandas dataframe
# and returns the modified dataframe as specified by the user's image.

def topCarModel(df):
    # Drop all the cars where units_sold_per_annum < 10000
    df = df[df['units_sold_per_annum'] >= 10000]

    # Clean the 'price' column such that it only contains numeric characters
    df['price'] = df['price'].replace('[^\d.]', '', regex=True).astype(float)
    
    # For each company, find the model for which it pays the highest yearly commission
    # The commission is calculated as Price*Units*Commission (%)
    df['total_commission'] = df['price'] * df['units_sold_per_annum'] * df['dealer_commission'] / 100
    
    # Sort and return the resultant dataframe in ascending order by company name
    result_df = df.loc[df.groupby('car_company')['total_commission'].idxmax()].sort_values('car_company')
    
    # Selecting only the required columns to match the final output structure as per the image
    result_df = result_df[['car_company', 'car_model']]

    return result_df

# Create the dataframe as given in the image to test the function
data = {
    'car_company': ['BMW', 'BMW', 'BMW', 'TOYOTA', 'FORD', 'HYUNDAI', 'MERCEDES'],
    'car_model': ['bmw_m1', 'bmw_m15', 'bmw_m2', 'toyota_m1', 'ford_m2', 'hyndai_m14', 'mercedes_m11'],
    'price': ['12000', '939300!!', '@#@100002', '44444@@#@$', '9!4@@@e@493', '566565AWW', '1111111ORP'],
    'units_sold_per_annum': [1, 75684, 955, 26000, 1544, 10000, 44000],
    'dealer_commission': [1.6, 1, 2.9, 1.5, 2.3, 4.4, 3.2]
}

df = pd.DataFrame(data)

# Apply the function to the dataframe
top_cars = topCarModel(df)
top_cars
Editor is loading...
Leave a Comment