{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport seaborn as sb\nimport plotly.express as px\nimport plotly.graph_objects as go\nimport warnings\nwarnings.filterwarnings(\"ignore\", category=DeprecationWarning)\nsb.set_style('darkgrid')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-03T20:06:50.932770Z","iopub.execute_input":"2023-01-03T20:06:50.933294Z","iopub.status.idle":"2023-01-03T20:06:50.946739Z","shell.execute_reply.started":"2023-01-03T20:06:50.933252Z","shell.execute_reply":"2023-01-03T20:06:50.945176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/us-used-cars-dataset/used_cars_data.csv' , nrows = 1000000 , usecols = ['back_legroom','body_type',\n  'city', 'city_fuel_economy','daysonmarket', 'engine_displacement', \n  'engine_type',  'fleet' , 'front_legroom', 'fuel_tank_volume', 'fuel_type', 'has_accidents', 'height',\n  'highway_fuel_economy', 'horsepower', 'isCab', 'is_new', 'latitude', 'length',\n   'longitude','major_options', 'make_name', 'maximum_seating',\n  'mileage', 'model_name', 'price', 'seller_rating',  'transmission_display',\n  'wheel_system_display', 'width', 'year'])\ndf.info()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T20:06:50.949666Z","iopub.execute_input":"2023-01-03T20:06:50.950871Z","iopub.status.idle":"2023-01-03T20:07:16.718844Z","shell.execute_reply.started":"2023-01-03T20:06:50.950814Z","shell.execute_reply":"2023-01-03T20:07:16.717288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.query('year >= 2000')\nround(df.select_dtypes(exclude = ['object']).describe() , 2)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T20:07:16.720822Z","iopub.execute_input":"2023-01-03T20:07:16.725383Z","iopub.status.idle":"2023-01-03T20:07:17.863563Z","shell.execute_reply.started":"2023-01-03T20:07:16.725322Z","shell.execute_reply":"2023-01-03T20:07:17.861881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Filling the Missing values & Data Preprocessing","metadata":{}},{"cell_type":"code","source":"\ndf['body_type'] = df['body_type'].fillna(df['body_type'].mode()[0])\ndf['mileage'] = df['mileage'].fillna(df['mileage'].mean())\n\ncfe = df.groupby(['city'])['city_fuel_economy'].median()\ndf['city_fuel_economy'] = df['city_fuel_economy'].fillna(df['city'].map(cfe.to_dict()))\n\nhfe = df.groupby(['body_type'])['highway_fuel_economy'].median().to_dict()\ndf['highway_fuel_economy'] = df['highway_fuel_economy'].fillna(df['body_type'].map(hfe))\n\nnullsrs = (df.isnull().mean()*100).sort_values(ascending = False)\nlst = nullsrs.loc[nullsrs > 40].index.to_list()\nlst\nfor col in lst:\n  df[col] = df[col].fillna(df[col].mode().values[0])\n\n\n# filling na values with mode & mean\nnullsrs = (df.isnull().mean()*100).sort_values(ascending = False)\ndel_lst = nullsrs.loc[nullsrs < 7.5].index.to_list()\nfor col in df[del_lst].select_dtypes(['object']).columns.to_list():\n  df[col] = df[col].fillna(df[col].mode()[0])\nfor col in df[del_lst].select_dtypes(['int64' , 'float64']).columns.to_list():\n  df[col] = df[col].fillna(df[col].mean())\n\n#  replacing '--' value in columns\nfor col in ['back_legroom' , 'front_legroom' , 'fuel_tank_volume' , 'length' , 'maximum_seating' , 'width' , 'height']:\n  df[col]= df[col].replace( '--' , df[col].mode()[0] )\n  df[col] = df[col].map(lambda x : str(x).split()[0]).astype('float')\n","metadata":{"execution":{"iopub.status.busy":"2023-01-03T20:07:17.867711Z","iopub.execute_input":"2023-01-03T20:07:17.869135Z","iopub.status.idle":"2023-01-03T20:07:31.457004Z","shell.execute_reply.started":"2023-01-03T20:07:17.869045Z","shell.execute_reply":"2023-01-03T20:07:31.455698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (25 , 10))\nsb.heatmap(df.select_dtypes('float' , 'int').corr() , annot = True)\nplt.xticks(rotation = 45);","metadata":{"execution":{"iopub.status.busy":"2023-01-03T20:07:31.458900Z","iopub.execute_input":"2023-01-03T20:07:31.459450Z","iopub.status.idle":"2023-01-03T20:07:34.420757Z","shell.execute_reply.started":"2023-01-03T20:07:31.459398Z","shell.execute_reply":"2023-01-03T20:07:34.419155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"b = df.groupby(['body_type' , 'year'])['body_type'].count().unstack().fillna(0)\nplt.figure(figsize = (25 , 7))\nplt.suptitle('Change in listings for different Body Types from 2000 - 2021' , weight = 'bold'  , fontsize  = 20)\nfor btype in b.index:\n    plt.plot( range(2000,2022) , b.loc[btype , : ] ,  marker = 'o'  , linestyle = '--' , label = btype)\nplt.xlabel('Years' , weight = 'bold')\nplt.ylabel('Count' , weight = 'bold')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T20:07:34.423277Z","iopub.execute_input":"2023-01-03T20:07:34.424440Z","iopub.status.idle":"2023-01-03T20:07:35.080390Z","shell.execute_reply.started":"2023-01-03T20:07:34.424378Z","shell.execute_reply":"2023-01-03T20:07:35.079141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = df.groupby(['body_type' , 'year'])['price'].mean().unstack().fillna(0)\nplt.figure(figsize = (25 , 7))\nplt.suptitle('Change in mean prices from 2000 - 2021' , weight = 'bold'  , fontsize  = 20)\nfor btype in a.index:\n  plt.plot( range(2000,2022) , a.loc[btype , : ] ,  marker = 'o'  , linestyle = '--' , label = btype)\nplt.xlabel('Years' , weight = 'bold')\nplt.ylabel('Mean Prices' , weight = 'bold')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T20:07:35.082342Z","iopub.execute_input":"2023-01-03T20:07:35.083711Z","iopub.status.idle":"2023-01-03T20:07:35.723464Z","shell.execute_reply.started":"2023-01-03T20:07:35.083651Z","shell.execute_reply":"2023-01-03T20:07:35.722147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cityfe = df.groupby(['year'])['city_fuel_economy'].mean()\nhighwayfe = df.groupby(['year'])['highway_fuel_economy'].mean()\nplt.figure(figsize = (25,5))\nplt.title('Comparing fuel economies for city & highway' , weight = 'bold', fontsize = 20)\nplt.plot(range(2000,2022) , cityfe.values , label = 'city' , marker = 'o')\nplt.plot(range(2000,2022) , highwayfe.values , label = 'highway' ,  marker = 'X')\nplt.xlabel('year' , weight = 'bold' , fontsize = 15)\nplt.ylabel('fuel economy' , weight = 'bold' , fontsize = 15)\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T20:07:35.725441Z","iopub.execute_input":"2023-01-03T20:07:35.726887Z","iopub.status.idle":"2023-01-03T20:07:36.164771Z","shell.execute_reply.started":"2023-01-03T20:07:35.726826Z","shell.execute_reply":"2023-01-03T20:07:36.161618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# TYPE VS MAX. SEATING\n\nseats_df = df.groupby(['maximum_seating' , 'body_type'])['body_type'].count().to_frame().rename(columns = {'body_type':'Count'}).reset_index()\nfig = px.bar(seats_df, x=\"body_type\", y=\"Count\", animation_frame=\"maximum_seating\", animation_group=\"body_type\",\n            color=\"Count\")\nfig[\"layout\"].pop(\"updatemenus\") # optional, drop animation buttons\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T20:07:36.167569Z","iopub.execute_input":"2023-01-03T20:07:36.172582Z","iopub.status.idle":"2023-01-03T20:07:36.498312Z","shell.execute_reply.started":"2023-01-03T20:07:36.172489Z","shell.execute_reply":"2023-01-03T20:07:36.496906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# CATEFORY VS ENGINE-TYPE\n\nlst1 = list(df['engine_type'].map( lambda x : x.split()[-1] if len(x.split()) > 1 else x).unique())\n\ngasoline = []\nothers = []\nfor val in lst1:\n    if (list(val)[-1].isnumeric() == True):\n        gasoline.append(val)\n    else:\n        others.append(val)\n        \ndef eng_type(fuel):\n    return df[df['engine_type'].\n           map(lambda x : x.endswith(fuel))]['engine_type'].\\\n           value_counts(). \\\n           to_frame().reset_index(). \\\n           rename(columns = {'index' : 'engine_type' , 'engine_type':'Count'})\n\ngasoline_df = df[df['engine_type'].map(lambda x : x in gasoline)]['engine_type'].value_counts().to_frame().reset_index().\\\n                                                           rename(columns = {'index' : 'engine_type' , 'engine_type':'Count'})\n\n\n# plotly\nfig = go.Figure()\n\n# set up ONE trace\nfig.add_trace(go.Line(x= eng_type('Diesel').engine_type.values, y=eng_type('Diesel').Count.values,visible=True))\n\nupdatemenu = []\nbuttons = []\n\n# button with one option for each dataframe\nfor col in others:\n    buttons.append(dict(method='restyle',\n                        label= 'Natural Gas' if col == 'Gas' else 'Flex Fuel' if col == 'Vehicle' else col,\n                        visible=True,\n                        args=[{'y':[eng_type(col).Count.values],\n                               'x':[eng_type(col).engine_type.values],\n                               'type':'line'}, [0]],\n                        )\n                  )\nbuttons.append(dict(method='restyle',\n                    label= 'Gasoline',\n                    visible=True,\n                    args=[{'y':[gasoline_df.Count.values],\n                           'x':[gasoline_df.engine_type.values],\n                           'type':'line'}, [0]],\n                        )\n                  )\n\n# some adjustments to the updatemenus\nupdatemenu = []\nyour_menu = dict()\nupdatemenu.append(your_menu)\n\nupdatemenu[0]['buttons'] = buttons\nupdatemenu[0]['direction'] = 'down'\nupdatemenu[0]['showactive'] = True\n\n# add dropdown menus to the figure\nfig.update_layout(showlegend=False, updatemenus=updatemenu)\nfig.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-01-03T20:07:36.504820Z","iopub.execute_input":"2023-01-03T20:07:36.505289Z","iopub.status.idle":"2023-01-03T20:07:41.904018Z","shell.execute_reply.started":"2023-01-03T20:07:36.505251Z","shell.execute_reply":"2023-01-03T20:07:41.902191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def b_type_price(mfg):\n    return df.query('make_name == @mfg').groupby('body_type')['price'].mean().to_frame().reset_index()\n\nb_type_price('Hyundai')\n\nfig = go.Figure()\nfig.add_trace(go.Bar(name = 'Dropdown-1', x = b_type_price('Hyundai').body_type , y = b_type_price('Hyundai').price))\n\nbuttons = []\n\n# button with one option for each dataframe\nfor col in df['make_name'].unique():\n    buttons.append(dict(method ='restyle',\n                        label =  col,\n                        visible = True,\n                        args=[{'y':[b_type_price(col).price.values],\n                               'x':[b_type_price(col).body_type.values],\n                               'type':'bar'}, [0]],\n                        )\n                  )\n\nfig.add_trace(go.Bar(name = 'Dropdown-2', x = b_type_price('Jeep').body_type , y = b_type_price('Jeep').price))\n\nbuttons2 = []\n\n# button with one option for each dataframe\nfor col in df['make_name'].unique():\n    buttons2.append(dict(method ='restyle',\n                        label =  col,\n                        visible = True,\n                        args=[{'y':[b_type_price(col).price.values],\n                               'x':[b_type_price(col).body_type.values],\n                               'type':'bar'}, [1]],\n                        )\n                  )\n    \nbutton_layer_1_height = 1.23\nupdatemenus = list([\n    dict(buttons=buttons,\n            direction=\"down\",\n            pad={\"r\": 10, \"t\": 10},\n            showactive=True,\n            x=0.11,\n            xanchor=\"left\",\n            y=button_layer_1_height,\n            yanchor=\"top\"),\n    dict(buttons=buttons2,\n            direction=\"down\",\n            pad={\"r\": 10, \"t\": 10},\n            showactive=True,\n            x=0.71,\n            xanchor=\"left\",\n            y=button_layer_1_height,\n            yanchor=\"top\")])\n    \nfig.update_layout( updatemenus=updatemenus)\n#added annotations next to dropdowns \nfig.update_layout(\n    annotations=[\n        dict(text=\"Dropdown-1\", x=0, xref=\"paper\", y=1.15, yref=\"paper\",\n                             align=\"left\", showarrow=False),\n        dict(text=\"Dropdown-2\", x=0.65, xref=\"paper\", y=1.15,\n                             yref=\"paper\", showarrow=False)\n    ])\n\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T20:07:41.912522Z","iopub.execute_input":"2023-01-03T20:07:41.916684Z","iopub.status.idle":"2023-01-03T20:07:55.791610Z","shell.execute_reply.started":"2023-01-03T20:07:41.916608Z","shell.execute_reply":"2023-01-03T20:07:55.790138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (25,6))\ndf.query( \"isCab == {} \".format(1) )['model_name'] \\\n                      .value_counts().head(30) \\\n                      .plot(kind = 'bar' ,rot = 45)\nplt.title('Top models for Cab' , weight = 'bold', fontsize = 20)\nplt.ylabel('count')\nplt.xlabel('manufacturers')","metadata":{"execution":{"iopub.status.busy":"2023-01-03T20:07:55.794066Z","iopub.execute_input":"2023-01-03T20:07:55.795144Z","iopub.status.idle":"2023-01-03T20:07:56.535558Z","shell.execute_reply.started":"2023-01-03T20:07:55.795060Z","shell.execute_reply":"2023-01-03T20:07:56.534237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# FEATURES IN THE VEHICLES IN NEW VEHICLES\nf_arr = df[df.is_new == True]['major_options'].apply(lambda x : eval(x)).values\nfeatures_lst = []\nfor val in f_arr:\n    for f in val:\n        features_lst.append(f)\n\nfeatures_st = set(features_lst)\nftrs = {}\nfor val in features_st:\n    ftrs[val] = 0\nfor val in features_st:\n    ftrs[val] = features_lst.count(val)\nfeatures_df = pd.DataFrame(ftrs.items() , columns = ['Features' , 'total_count']).sort_values('total_count' , ascending  = False).reset_index(drop = True)\nfig = px.bar(features_df, y ='total_count', x='Features')\nfig.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-01-03T20:07:56.538130Z","iopub.execute_input":"2023-01-03T20:07:56.539199Z","iopub.status.idle":"2023-01-03T20:08:12.353584Z","shell.execute_reply.started":"2023-01-03T20:07:56.539139Z","shell.execute_reply":"2023-01-03T20:08:12.352005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sb.scatterplot(data = df[['daysonmarket','price']] , x = 'daysonmarket' , y = 'price')\nfig = px.scatter(df[['daysonmarket','price']], x=\"daysonmarket\", y=\"price\",size=\"price\",size_max=60)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T20:08:12.355593Z","iopub.execute_input":"2023-01-03T20:08:12.356919Z","iopub.status.idle":"2023-01-03T20:08:13.057953Z","shell.execute_reply.started":"2023-01-03T20:08:12.356856Z","shell.execute_reply":"2023-01-03T20:08:13.055976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}