{ "cells": [ { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/var/folders/hq/f_rzh7j51m1ddfp3xw6s1qsr0000gn/T/ipykernel_83151/2512128407.py:6: DtypeWarning:\n", "\n", "Columns (16) have mixed types. Specify dtype option on import or set low_memory=False.\n", "\n" ] } ], "source": [ "import pandas as pd\n", "import seaborn as sns\n", "import plotly.express as px\n", "import plotly.graph_objects as go\n", "\n", "df = pd.read_csv('../complaints.csv')\n", "df['Date received'] = pd.to_datetime(df['Date received'])\n", "\n", "cols_to_consider = ['Product','Sub-product','Issue','Sub-issue','Consumer complaint narrative','Company public response','Company',\n", " 'State', 'ZIP code', 'Date received']\n", "df_new = df[cols_to_consider]\n", "\n", "df_new = df_new.dropna()\n", "\n", "\n", "product_map = {'Credit reporting or other personal consumer reports' : 'Credit Reporting',\n", " 'Credit reporting, credit repair services, or other personal consumer reports' : 'Credit Reporting',\n", " 'Payday loan, title loan, personal loan, or advance loan' : 'Loans / Mortgage',\n", " 'Payday loan, title loan, or personal loan' : 'Loans / Mortgage',\n", " 'Student loan' : 'Loans / Mortgage',\n", " 'Vehicle loan or lease' : 'Loans / Mortgage',\n", " 'Debt collection' : 'Debt collection',\n", " 'Credit card or prepaid card' : 'Credit/Prepaid Card',\n", " 'Credit card' : 'Credit/Prepaid Card',\n", " 'Prepaid card' : 'Credit/Prepaid Card',\n", " 'Mortgage' : 'Loans / Mortgage',\n", " 'Checking or savings account' : 'Checking or savings account' \n", " }\n", "\n", "df_new.loc[:,'Product'] = df_new['Product'].map(product_map)\n", "\n", "\n", "df_new['complaint length'] = df_new['Consumer complaint narrative'].apply(lambda x : len(x))\n", "df_new = df_new[df_new['complaint length'] > 20]\n", "\n", "complaints_to_exclude = ['See document attached', 'See the attached documents.', 'Incorrect information on my credit report', 'incorrect information on my credit report',\n", "'please see attached file','Please see documents Attached','Incorrect information on my credit report.', 'Please see attached file', 'see attached',\n", "'See attached', 'SEE ATTACHED DOCUMENTS', 'See Attached', 'SEE ATTACHMENT', 'SEE ATTACHMENTS', \n", "'XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX']\n", "\n", "df_new = df_new[~df_new['Consumer complaint narrative'].isin(complaints_to_exclude)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1. Top 5 common products" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "config": { "plotlyServerURL": "https://plot.ly" }, "data": [ { "alignmentgroup": "True", "hovertemplate": "Product=%{y}
Count=%{x}", "legendgroup": "Credit Reporting", "marker": { "color": "#636efa", "pattern": { "shape": "" } }, "name": "Credit Reporting", "offsetgroup": "Credit Reporting", "orientation": "h", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ 554471 ], "xaxis": "x", "y": [ "Credit Reporting" ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Product=%{y}
Count=%{x}", "legendgroup": "Debt collection", "marker": { "color": "#EF553B", "pattern": { "shape": "" } }, "name": "Debt collection", "offsetgroup": "Debt collection", "orientation": "h", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ 90714 ], "xaxis": "x", "y": [ "Debt collection" ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Product=%{y}
Count=%{x}", "legendgroup": "Credit/Prepaid Card", "marker": { "color": "#00cc96", "pattern": { "shape": "" } }, "name": "Credit/Prepaid Card", "offsetgroup": "Credit/Prepaid Card", "orientation": "h", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ 57169 ], "xaxis": "x", "y": [ "Credit/Prepaid Card" ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Product=%{y}
Count=%{x}", "legendgroup": "Checking or savings account", "marker": { "color": "#ab63fa", "pattern": { "shape": "" } }, "name": "Checking or savings account", "offsetgroup": "Checking or savings account", "orientation": "h", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ 46679 ], "xaxis": "x", "y": [ "Checking or savings account" ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Product=%{y}
Count=%{x}", "legendgroup": "Loans / Mortgage", "marker": { "color": "#FFA15A", "pattern": { "shape": "" } }, "name": "Loans / Mortgage", "offsetgroup": "Loans / Mortgage", "orientation": "h", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ 24014 ], "xaxis": "x", "y": [ "Loans / Mortgage" ], "yaxis": "y" } ], "layout": { "barmode": "relative", "legend": { "title": { "text": "Product" }, "tracegroupgap": 0 }, "showlegend": false, "template": { "data": { "bar": [ { "error_x": { "color": "#2a3f5f" }, "error_y": { "color": "#2a3f5f" }, "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "bar" } ], "barpolar": [ { "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "barpolar" } ], "carpet": [ { "aaxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "baxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "type": "carpet" } ], "choropleth": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "choropleth" } ], "contour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "contour" } ], "contourcarpet": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "contourcarpet" } ], "heatmap": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmap" } ], "heatmapgl": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmapgl" } ], "histogram": [ { "marker": { "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "histogram" } ], "histogram2d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2d" } ], "histogram2dcontour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2dcontour" } ], "mesh3d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "mesh3d" } ], "parcoords": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "parcoords" } ], "pie": [ { "automargin": true, "type": "pie" } ], "scatter": [ { "fillpattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 }, "type": "scatter" } ], "scatter3d": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter3d" } ], "scattercarpet": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattercarpet" } ], "scattergeo": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergeo" } ], "scattergl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergl" } ], "scattermapbox": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattermapbox" } ], "scatterpolar": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolar" } ], "scatterpolargl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolargl" } ], "scatterternary": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterternary" } ], "surface": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "surface" } ], "table": [ { "cells": { "fill": { "color": "#EBF0F8" }, "line": { "color": "white" } }, "header": { "fill": { "color": "#C8D4E3" }, "line": { "color": "white" } }, "type": "table" } ] }, "layout": { "annotationdefaults": { "arrowcolor": "#2a3f5f", "arrowhead": 0, "arrowwidth": 1 }, "autotypenumbers": "strict", "coloraxis": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "colorscale": { "diverging": [ [ 0, "#8e0152" ], [ 0.1, "#c51b7d" ], [ 0.2, "#de77ae" ], [ 0.3, "#f1b6da" ], [ 0.4, "#fde0ef" ], [ 0.5, "#f7f7f7" ], [ 0.6, "#e6f5d0" ], [ 0.7, "#b8e186" ], [ 0.8, "#7fbc41" ], [ 0.9, "#4d9221" ], [ 1, "#276419" ] ], "sequential": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "sequentialminus": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ] }, "colorway": [ "#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880", "#FF97FF", "#FECB52" ], "font": { "color": "#2a3f5f" }, "geo": { "bgcolor": "white", "lakecolor": "white", "landcolor": "#E5ECF6", "showlakes": true, "showland": true, "subunitcolor": "white" }, "hoverlabel": { "align": "left" }, "hovermode": "closest", "mapbox": { "style": "light" }, "paper_bgcolor": "white", "plot_bgcolor": "#E5ECF6", "polar": { "angularaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "radialaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "scene": { "xaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "yaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "zaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" } }, "shapedefaults": { "line": { "color": "#2a3f5f" } }, "ternary": { "aaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "baxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "caxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "title": { "x": 0.05 }, "xaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 }, "yaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 } } }, "title": { "text": "Top 5 Most Common Products" }, "xaxis": { "anchor": "y", "domain": [ 0, 1 ], "title": { "text": "Count" } }, "yaxis": { "anchor": "x", "categoryarray": [ "Loans / Mortgage", "Checking or savings account", "Credit/Prepaid Card", "Debt collection", "Credit Reporting" ], "categoryorder": "array", "domain": [ 0, 1 ], "title": { "text": "Product" } } } } }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Function to plot top n most common categories, with horizontal bars, colored bars, no legend, using seaborn palettes for colors\n", "def plot_top_n(df, column, title, n=5, palette_name=None):\n", " # Generate a color sequence from the seaborn palette\n", " color_sequence = sns.color_palette(palette_name, n_colors=n).as_hex() if palette_name else None\n", " \n", " # Get top n most common values in the specified column\n", " counts = df[column].value_counts().reset_index()\n", " counts.columns = [column, 'Count']\n", " top_n = counts.head(n)\n", " \n", " # Create a horizontal bar plot with the seaborn color sequence and remove the legend\n", " fig = px.bar(top_n, y=column, x='Count', title=title, orientation='h', \n", " color=column, color_discrete_sequence=color_sequence)\n", " fig.update_layout(showlegend=False)\n", " \n", " # Show the plot\n", " fig.show()\n", "\n", "# Example usage with seaborn palettes\n", "# Plotting top 5 most common products using the 'viridis' palette\n", "plot_top_n(df_new, 'Product', 'Top 5 Most Common Products', n=5, palette_name=None)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2. Plotting top 5 most common issues." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "config": { "plotlyServerURL": "https://plot.ly" }, "data": [ { "alignmentgroup": "True", "hovertemplate": "Issue=%{y}
Count=%{x}", "legendgroup": "Incorrect information on your report", "marker": { "color": "#5c01a6", "pattern": { "shape": "" } }, "name": "Incorrect information on your report", "offsetgroup": "Incorrect information on your report", "orientation": "h", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ 249082 ], "xaxis": "x", "y": [ "Incorrect information on your report" ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=%{y}
Count=%{x}", "legendgroup": "Problem with a credit reporting company's investigation into an existing problem", "marker": { "color": "#9c179e", "pattern": { "shape": "" } }, "name": "Problem with a credit reporting company's investigation into an existing problem", "offsetgroup": "Problem with a credit reporting company's investigation into an existing problem", "orientation": "h", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ 148276 ], "xaxis": "x", "y": [ "Problem with a credit reporting company's investigation into an existing problem" ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=%{y}
Count=%{x}", "legendgroup": "Improper use of your report", "marker": { "color": "#cc4778", "pattern": { "shape": "" } }, "name": "Improper use of your report", "offsetgroup": "Improper use of your report", "orientation": "h", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ 134074 ], "xaxis": "x", "y": [ "Improper use of your report" ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=%{y}
Count=%{x}", "legendgroup": "Attempts to collect debt not owed", "marker": { "color": "#ed7953", "pattern": { "shape": "" } }, "name": "Attempts to collect debt not owed", "offsetgroup": "Attempts to collect debt not owed", "orientation": "h", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ 36033 ], "xaxis": "x", "y": [ "Attempts to collect debt not owed" ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=%{y}
Count=%{x}", "legendgroup": "Managing an account", "marker": { "color": "#fdb42f", "pattern": { "shape": "" } }, "name": "Managing an account", "offsetgroup": "Managing an account", "orientation": "h", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ 25245 ], "xaxis": "x", "y": [ "Managing an account" ], "yaxis": "y" } ], "layout": { "barmode": "relative", "legend": { "title": { "text": "Issue" }, "tracegroupgap": 0 }, "showlegend": false, "template": { "data": { "bar": [ { "error_x": { "color": "#2a3f5f" }, "error_y": { "color": "#2a3f5f" }, "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "bar" } ], "barpolar": [ { "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "barpolar" } ], "carpet": [ { "aaxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "baxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "type": "carpet" } ], "choropleth": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "choropleth" } ], "contour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "contour" } ], "contourcarpet": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "contourcarpet" } ], "heatmap": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmap" } ], "heatmapgl": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmapgl" } ], "histogram": [ { "marker": { "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "histogram" } ], "histogram2d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2d" } ], "histogram2dcontour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2dcontour" } ], "mesh3d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "mesh3d" } ], "parcoords": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "parcoords" } ], "pie": [ { "automargin": true, "type": "pie" } ], "scatter": [ { "fillpattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 }, "type": "scatter" } ], "scatter3d": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter3d" } ], "scattercarpet": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattercarpet" } ], "scattergeo": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergeo" } ], "scattergl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergl" } ], "scattermapbox": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattermapbox" } ], "scatterpolar": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolar" } ], "scatterpolargl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolargl" } ], "scatterternary": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterternary" } ], "surface": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "surface" } ], "table": [ { "cells": { "fill": { "color": "#EBF0F8" }, "line": { "color": "white" } }, "header": { "fill": { "color": "#C8D4E3" }, "line": { "color": "white" } }, "type": "table" } ] }, "layout": { "annotationdefaults": { "arrowcolor": "#2a3f5f", "arrowhead": 0, "arrowwidth": 1 }, "autotypenumbers": "strict", "coloraxis": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "colorscale": { "diverging": [ [ 0, "#8e0152" ], [ 0.1, "#c51b7d" ], [ 0.2, "#de77ae" ], [ 0.3, "#f1b6da" ], [ 0.4, "#fde0ef" ], [ 0.5, "#f7f7f7" ], [ 0.6, "#e6f5d0" ], [ 0.7, "#b8e186" ], [ 0.8, "#7fbc41" ], [ 0.9, "#4d9221" ], [ 1, "#276419" ] ], "sequential": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "sequentialminus": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ] }, "colorway": [ "#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880", "#FF97FF", "#FECB52" ], "font": { "color": "#2a3f5f" }, "geo": { "bgcolor": "white", "lakecolor": "white", "landcolor": "#E5ECF6", "showlakes": true, "showland": true, "subunitcolor": "white" }, "hoverlabel": { "align": "left" }, "hovermode": "closest", "mapbox": { "style": "light" }, "paper_bgcolor": "white", "plot_bgcolor": "#E5ECF6", "polar": { "angularaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "radialaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "scene": { "xaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "yaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "zaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" } }, "shapedefaults": { "line": { "color": "#2a3f5f" } }, "ternary": { "aaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "baxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "caxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "title": { "x": 0.05 }, "xaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 }, "yaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 } } }, "title": { "text": "Top 5 Most Common Issues" }, "xaxis": { "anchor": "y", "domain": [ 0, 1 ], "title": { "text": "Count" } }, "yaxis": { "anchor": "x", "categoryarray": [ "Managing an account", "Attempts to collect debt not owed", "Improper use of your report", "Problem with a credit reporting company's investigation into an existing problem", "Incorrect information on your report" ], "categoryorder": "array", "domain": [ 0, 1 ], "title": { "text": "Issue" } } } } }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Plotting top 5 most common issues using the 'plasma' palette\n", "plot_top_n(df_new, 'Issue', 'Top 5 Most Common Issues', n=5, palette_name=\"plasma\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3. Top 5 Issues in Each Product Category\n" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/var/folders/hq/f_rzh7j51m1ddfp3xw6s1qsr0000gn/T/ipykernel_83151/1607134567.py:11: DeprecationWarning:\n", "\n", "DataFrameGroupBy.apply operated on the grouping columns. This behavior is deprecated, and in a future version of pandas the grouping columns will be excluded from the operation. Either pass `include_groups=False` to exclude the groupings or explicitly select the grouping columns after groupby to silence this warning.\n", "\n" ] }, { "data": { "application/vnd.plotly.v1+json": { "config": { "plotlyServerURL": "https://plot.ly" }, "data": [ { "alignmentgroup": "True", "hovertemplate": "Issue=Incorrect information on your report
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "Incorrect information on your report", "marker": { "color": "#636efa", "pattern": { "shape": "" } }, "name": "Incorrect information on your report", "offsetgroup": "Incorrect information on your report", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Credit Reporting", "Loans / Mortgage" ], "xaxis": "x", "y": [ 245271, 2116 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=Problem with a credit reporting company's investigation into an existing problem
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "Problem with a credit reporting company's investigation into an existing problem", "marker": { "color": "#EF553B", "pattern": { "shape": "" } }, "name": "Problem with a credit reporting company's investigation into an existing problem", "offsetgroup": "Problem with a credit reporting company's investigation into an existing problem", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Credit Reporting" ], "xaxis": "x", "y": [ 144151 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=Improper use of your report
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "Improper use of your report", "marker": { "color": "#00cc96", "pattern": { "shape": "" } }, "name": "Improper use of your report", "offsetgroup": "Improper use of your report", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Credit Reporting" ], "xaxis": "x", "y": [ 133342 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=Problem with a company's investigation into an existing problem
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "Problem with a company's investigation into an existing problem", "marker": { "color": "#ab63fa", "pattern": { "shape": "" } }, "name": "Problem with a company's investigation into an existing problem", "offsetgroup": "Problem with a company's investigation into an existing problem", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Credit Reporting" ], "xaxis": "x", "y": [ 21007 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=Unable to get your credit report or credit score
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "Unable to get your credit report or credit score", "marker": { "color": "#FFA15A", "pattern": { "shape": "" } }, "name": "Unable to get your credit report or credit score", "offsetgroup": "Unable to get your credit report or credit score", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Credit Reporting" ], "xaxis": "x", "y": [ 5687 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=Attempts to collect debt not owed
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "Attempts to collect debt not owed", "marker": { "color": "#19d3f3", "pattern": { "shape": "" } }, "name": "Attempts to collect debt not owed", "offsetgroup": "Attempts to collect debt not owed", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Debt collection" ], "xaxis": "x", "y": [ 36033 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=Written notification about debt
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "Written notification about debt", "marker": { "color": "#FF6692", "pattern": { "shape": "" } }, "name": "Written notification about debt", "offsetgroup": "Written notification about debt", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Debt collection" ], "xaxis": "x", "y": [ 14576 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=False statements or representation
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "False statements or representation", "marker": { "color": "#B6E880", "pattern": { "shape": "" } }, "name": "False statements or representation", "offsetgroup": "False statements or representation", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Debt collection" ], "xaxis": "x", "y": [ 9176 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=Communication tactics
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "Communication tactics", "marker": { "color": "#FF97FF", "pattern": { "shape": "" } }, "name": "Communication tactics", "offsetgroup": "Communication tactics", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Debt collection" ], "xaxis": "x", "y": [ 8366 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=Cont'd attempts collect debt not owed
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "Cont'd attempts collect debt not owed", "marker": { "color": "#FECB52", "pattern": { "shape": "" } }, "name": "Cont'd attempts collect debt not owed", "offsetgroup": "Cont'd attempts collect debt not owed", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Debt collection" ], "xaxis": "x", "y": [ 7926 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=Problem with a purchase shown on your statement
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "Problem with a purchase shown on your statement", "marker": { "color": "#636efa", "pattern": { "shape": "" } }, "name": "Problem with a purchase shown on your statement", "offsetgroup": "Problem with a purchase shown on your statement", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Credit/Prepaid Card" ], "xaxis": "x", "y": [ 14103 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=Other features, terms, or problems
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "Other features, terms, or problems", "marker": { "color": "#EF553B", "pattern": { "shape": "" } }, "name": "Other features, terms, or problems", "offsetgroup": "Other features, terms, or problems", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Credit/Prepaid Card" ], "xaxis": "x", "y": [ 6386 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=Fees or interest
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "Fees or interest", "marker": { "color": "#00cc96", "pattern": { "shape": "" } }, "name": "Fees or interest", "offsetgroup": "Fees or interest", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Credit/Prepaid Card" ], "xaxis": "x", "y": [ 6004 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=Getting a credit card
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "Getting a credit card", "marker": { "color": "#ab63fa", "pattern": { "shape": "" } }, "name": "Getting a credit card", "offsetgroup": "Getting a credit card", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Credit/Prepaid Card" ], "xaxis": "x", "y": [ 5292 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=Problem when making payments
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "Problem when making payments", "marker": { "color": "#FFA15A", "pattern": { "shape": "" } }, "name": "Problem when making payments", "offsetgroup": "Problem when making payments", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Credit/Prepaid Card" ], "xaxis": "x", "y": [ 4615 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=Managing an account
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "Managing an account", "marker": { "color": "#19d3f3", "pattern": { "shape": "" } }, "name": "Managing an account", "offsetgroup": "Managing an account", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Checking or savings account" ], "xaxis": "x", "y": [ 25245 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=Closing an account
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "Closing an account", "marker": { "color": "#FF6692", "pattern": { "shape": "" } }, "name": "Closing an account", "offsetgroup": "Closing an account", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Checking or savings account" ], "xaxis": "x", "y": [ 6510 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=Problem with a lender or other company charging your account
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "Problem with a lender or other company charging your account", "marker": { "color": "#B6E880", "pattern": { "shape": "" } }, "name": "Problem with a lender or other company charging your account", "offsetgroup": "Problem with a lender or other company charging your account", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Checking or savings account" ], "xaxis": "x", "y": [ 5506 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=Opening an account
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "Opening an account", "marker": { "color": "#FF97FF", "pattern": { "shape": "" } }, "name": "Opening an account", "offsetgroup": "Opening an account", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Checking or savings account" ], "xaxis": "x", "y": [ 5452 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=Problem caused by your funds being low
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "Problem caused by your funds being low", "marker": { "color": "#FECB52", "pattern": { "shape": "" } }, "name": "Problem caused by your funds being low", "offsetgroup": "Problem caused by your funds being low", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Checking or savings account" ], "xaxis": "x", "y": [ 3763 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=Dealing with your lender or servicer
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "Dealing with your lender or servicer", "marker": { "color": "#636efa", "pattern": { "shape": "" } }, "name": "Dealing with your lender or servicer", "offsetgroup": "Dealing with your lender or servicer", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Loans / Mortgage" ], "xaxis": "x", "y": [ 4114 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=Managing the loan or lease
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "Managing the loan or lease", "marker": { "color": "#EF553B", "pattern": { "shape": "" } }, "name": "Managing the loan or lease", "offsetgroup": "Managing the loan or lease", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Loans / Mortgage" ], "xaxis": "x", "y": [ 4092 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=Problems at the end of the loan or lease
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "Problems at the end of the loan or lease", "marker": { "color": "#00cc96", "pattern": { "shape": "" } }, "name": "Problems at the end of the loan or lease", "offsetgroup": "Problems at the end of the loan or lease", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Loans / Mortgage" ], "xaxis": "x", "y": [ 2309 ], "yaxis": "y" }, { "alignmentgroup": "True", "hovertemplate": "Issue=Struggling to pay your loan
Product=%{x}
Number of Complaints=%{y}", "legendgroup": "Struggling to pay your loan", "marker": { "color": "#ab63fa", "pattern": { "shape": "" } }, "name": "Struggling to pay your loan", "offsetgroup": "Struggling to pay your loan", "orientation": "v", "showlegend": true, "textposition": "auto", "type": "bar", "x": [ "Loans / Mortgage" ], "xaxis": "x", "y": [ 2073 ], "yaxis": "y" } ], "layout": { "barmode": "relative", "height": 600, "legend": { "title": { "text": "Issue" }, "tracegroupgap": 0 }, "showlegend": false, "template": { "data": { "bar": [ { "error_x": { "color": "#2a3f5f" }, "error_y": { "color": "#2a3f5f" }, "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "bar" } ], "barpolar": [ { "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "barpolar" } ], "carpet": [ { "aaxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "baxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "type": "carpet" } ], "choropleth": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "choropleth" } ], "contour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "contour" } ], "contourcarpet": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "contourcarpet" } ], "heatmap": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmap" } ], "heatmapgl": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmapgl" } ], "histogram": [ { "marker": { "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "histogram" } ], "histogram2d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2d" } ], "histogram2dcontour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2dcontour" } ], "mesh3d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "mesh3d" } ], "parcoords": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "parcoords" } ], "pie": [ { "automargin": true, "type": "pie" } ], "scatter": [ { "fillpattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 }, "type": "scatter" } ], "scatter3d": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter3d" } ], "scattercarpet": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattercarpet" } ], "scattergeo": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergeo" } ], "scattergl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergl" } ], "scattermapbox": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattermapbox" } ], "scatterpolar": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolar" } ], "scatterpolargl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolargl" } ], "scatterternary": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterternary" } ], "surface": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "surface" } ], "table": [ { "cells": { "fill": { "color": "#EBF0F8" }, "line": { "color": "white" } }, "header": { "fill": { "color": "#C8D4E3" }, "line": { "color": "white" } }, "type": "table" } ] }, "layout": { "annotationdefaults": { "arrowcolor": "#2a3f5f", "arrowhead": 0, "arrowwidth": 1 }, "autotypenumbers": "strict", "coloraxis": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "colorscale": { "diverging": [ [ 0, "#8e0152" ], [ 0.1, "#c51b7d" ], [ 0.2, "#de77ae" ], [ 0.3, "#f1b6da" ], [ 0.4, "#fde0ef" ], [ 0.5, "#f7f7f7" ], [ 0.6, "#e6f5d0" ], [ 0.7, "#b8e186" ], [ 0.8, "#7fbc41" ], [ 0.9, "#4d9221" ], [ 1, "#276419" ] ], "sequential": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "sequentialminus": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ] }, "colorway": [ "#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880", "#FF97FF", "#FECB52" ], "font": { "color": "#2a3f5f" }, "geo": { "bgcolor": "white", "lakecolor": "white", "landcolor": "#E5ECF6", "showlakes": true, "showland": true, "subunitcolor": "white" }, "hoverlabel": { "align": "left" }, "hovermode": "closest", "mapbox": { "style": "light" }, "paper_bgcolor": "white", "plot_bgcolor": "#E5ECF6", "polar": { "angularaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "radialaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "scene": { "xaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "yaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "zaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" } }, "shapedefaults": { "line": { "color": "#2a3f5f" } }, "ternary": { "aaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "baxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "caxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "title": { "x": 0.05 }, "xaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 }, "yaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 } } }, "title": { "text": "Top 5 Issues in Each Product Category" }, "width": 900, "xaxis": { "anchor": "y", "categoryarray": [ "Credit Reporting", "Debt collection", "Credit/Prepaid Card", "Checking or savings account", "Loans / Mortgage" ], "categoryorder": "array", "domain": [ 0, 1 ], "title": { "text": "Product" } }, "yaxis": { "anchor": "x", "domain": [ 0, 1 ], "title": { "text": "Number of Complaints" } } } } }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Step 1: Group data by 'Product' and 'Issue', then count occurrences\n", "grouped_data = df_new.groupby(['Product', 'Issue']).size().reset_index(name='Count')\n", "\n", "# Calculate total issues per product for ordering\n", "total_issues_per_product = grouped_data.groupby('Product')['Count'].sum().reset_index(name='TotalIssues')\n", "\n", "# Sort products by total issues in descending order\n", "sorted_products = total_issues_per_product.sort_values('TotalIssues', ascending=False)\n", "\n", "# Step 2: Get top 5 issues for each product sorted by 'Count' in descending order\n", "top_issues_per_product = (grouped_data.groupby('Product', as_index=False)\n", " .apply(lambda x: x.nlargest(5, 'Count'))\n", " .reset_index(drop=True))\n", "\n", "# Merge to get the order column (TotalIssues) in top_issues_per_product for sorting\n", "top_issues_per_product = top_issues_per_product.merge(sorted_products, on='Product')\n", "\n", "# Sort top_issues_per_product DataFrame based on TotalIssues column to ensure the plot respects this order\n", "top_issues_per_product = top_issues_per_product.sort_values(by=['TotalIssues', 'Count'], ascending=[False, False])\n", "\n", "# Step 3: Create a vertical stacked bar chart\n", "fig = px.bar(top_issues_per_product, x='Product', y='Count', color='Issue',\n", " title='Top 5 Issues in Each Product Category',\n", " labels={'Count': 'Number of Complaints'}, \n", " category_orders={'Product': sorted_products['Product'].tolist()}) # Explicitly set the order of products\n", "\n", "# Update layout to remove legend and adjust dimensions for clarity\n", "fig.update_layout(showlegend=False, width=900, height=600)\n", "\n", "# Display the plot\n", "fig.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4. Companies with the Most Complaints in 2023" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "config": { "plotlyServerURL": "https://plot.ly" }, "data": [ { "alignmentgroup": "True", "hovertemplate": "Number of Complaints=%{x}
Company=%{y}
color=%{marker.color}", "legendgroup": "", "marker": { "color": [ 1558, 1789, 2086, 2258, 3817, 4986, 6640, 8967, 99838, 109194 ], "coloraxis": "coloraxis", "pattern": { "shape": "" } }, "name": "", "offsetgroup": "", "orientation": "h", "showlegend": false, "textposition": "auto", "type": "bar", "x": [ 1558, 1789, 2086, 2258, 3817, 4986, 6640, 8967, 99838, 109194 ], "xaxis": "x", "y": [ "ALLY FINANCIAL INC.", "NAVY FEDERAL CREDIT UNION", "U.S. BANCORP", "MOHELA", "SYNCHRONY FINANCIAL", "CITIBANK, N.A.", "BANK OF AMERICA, NATIONAL ASSOCIATION", "WELLS FARGO & COMPANY", "Experian Information Solutions Inc.", "TRANSUNION INTERMEDIATE HOLDINGS, INC." ], "yaxis": "y" } ], "layout": { "barmode": "relative", "coloraxis": { "colorbar": { "title": { "text": "color" } }, "colorscale": [ [ 0, "green" ], [ 0.05, "yellow" ], [ 1, "red" ] ], "showscale": false }, "height": 500, "legend": { "tracegroupgap": 0 }, "template": { "data": { "bar": [ { "error_x": { "color": "#2a3f5f" }, "error_y": { "color": "#2a3f5f" }, "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "bar" } ], "barpolar": [ { "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "barpolar" } ], "carpet": [ { "aaxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "baxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "type": "carpet" } ], "choropleth": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "choropleth" } ], "contour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "contour" } ], "contourcarpet": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "contourcarpet" } ], "heatmap": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmap" } ], "heatmapgl": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmapgl" } ], "histogram": [ { "marker": { "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "histogram" } ], "histogram2d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2d" } ], "histogram2dcontour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2dcontour" } ], "mesh3d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "mesh3d" } ], "parcoords": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "parcoords" } ], "pie": [ { "automargin": true, "type": "pie" } ], "scatter": [ { "fillpattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 }, "type": "scatter" } ], "scatter3d": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter3d" } ], "scattercarpet": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattercarpet" } ], "scattergeo": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergeo" } ], "scattergl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergl" } ], "scattermapbox": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattermapbox" } ], "scatterpolar": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolar" } ], "scatterpolargl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolargl" } ], "scatterternary": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterternary" } ], "surface": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "surface" } ], "table": [ { "cells": { "fill": { "color": "#EBF0F8" }, "line": { "color": "white" } }, "header": { "fill": { "color": "#C8D4E3" }, "line": { "color": "white" } }, "type": "table" } ] }, "layout": { "annotationdefaults": { "arrowcolor": "#2a3f5f", "arrowhead": 0, "arrowwidth": 1 }, "autotypenumbers": "strict", "coloraxis": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "colorscale": { "diverging": [ [ 0, "#8e0152" ], [ 0.1, "#c51b7d" ], [ 0.2, "#de77ae" ], [ 0.3, "#f1b6da" ], [ 0.4, "#fde0ef" ], [ 0.5, "#f7f7f7" ], [ 0.6, "#e6f5d0" ], [ 0.7, "#b8e186" ], [ 0.8, "#7fbc41" ], [ 0.9, "#4d9221" ], [ 1, "#276419" ] ], "sequential": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "sequentialminus": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ] }, "colorway": [ "#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880", "#FF97FF", "#FECB52" ], "font": { "color": "#2a3f5f" }, "geo": { "bgcolor": "white", "lakecolor": "white", "landcolor": "#E5ECF6", "showlakes": true, "showland": true, "subunitcolor": "white" }, "hoverlabel": { "align": "left" }, "hovermode": "closest", "mapbox": { "style": "light" }, "paper_bgcolor": "white", "plot_bgcolor": "#E5ECF6", "polar": { "angularaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "radialaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "scene": { "xaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "yaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "zaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" } }, "shapedefaults": { "line": { "color": "#2a3f5f" } }, "ternary": { "aaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "baxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "caxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "title": { "x": 0.05 }, "xaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 }, "yaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 } } }, "title": { "text": "Top 10 Companies with the Most Complaints in 2023" }, "width": 800, "xaxis": { "anchor": "y", "domain": [ 0, 1 ], "title": { "text": "Number of Complaints" } }, "yaxis": { "anchor": "x", "domain": [ 0, 1 ], "tickfont": { "size": 10 }, "title": { "text": "Company" } } } } }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Filter data for the year 2023\n", "df_2023 = df_new[df_new['Date received'].dt.year == 2023]\n", "\n", "# Group data by company name and count the number of complaints for each company\n", "company_complaint_counts = df_2023['Company'].value_counts()\n", "\n", "top_n = 10\n", "# Ensure the companies are sorted in ascending order for correct plotting\n", "top_companies = company_complaint_counts.head(top_n).sort_values(ascending=True)\n", "\n", "# Create a horizontal bar chart using Plotly Express with a nicer color scale\n", "fig = px.bar(\n", " x=top_companies.values,\n", " y=top_companies.index,\n", " orientation='h',\n", " color=top_companies.values, # This assigns a color based on the value\n", " color_continuous_scale=[(0.0, \"green\"),\n", " (0.05, \"yellow\"),\n", " (1.0, \"red\")], # This is an example of a nice color scale\n", " title=f'Top {top_n} Companies with the Most Complaints in 2023',\n", " labels={'x': 'Number of Complaints', 'y': 'Company'}\n", ")\n", "\n", "fig.update_layout(\n", " xaxis=dict(\n", " title='Number of Complaints',\n", " ),\n", " yaxis=dict(\n", " tickfont=dict(size=10),\n", " ),\n", " height=500,\n", " width=800,\n", ")\n", "\n", "# To display a color bar, showing the mapping of colors to values\n", "fig.update_layout(coloraxis_showscale=False)\n", "\n", "fig.show()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5. Top 10 states with most complaints" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "config": { "plotlyServerURL": "https://plot.ly" }, "data": [ { "alignmentgroup": "True", "hovertemplate": "State=%{x}
Number of Complaints=%{y}
color=%{marker.color}", "legendgroup": "", "marker": { "color": [ 97310, 88908, 82304, 57874, 45104, 36408, 33498, 28462, 24951, 19240 ], "coloraxis": "coloraxis", "pattern": { "shape": "" } }, "name": "", "offsetgroup": "", "orientation": "v", "showlegend": false, "textposition": "auto", "type": "bar", "x": [ "Florida", "California", "Texas", "Georgia", "New York", "Illinois", "Pennsylvania", "North Carolina", "New Jersey", "Maryland" ], "xaxis": "x", "y": [ 97310, 88908, 82304, 57874, 45104, 36408, 33498, 28462, 24951, 19240 ], "yaxis": "y" } ], "layout": { "barmode": "relative", "coloraxis": { "colorbar": { "title": { "text": "color" } }, "colorscale": [ [ 0, "#30123b" ], [ 0.07142857142857142, "#4145ab" ], [ 0.14285714285714285, "#4675ed" ], [ 0.21428571428571427, "#39a2fc" ], [ 0.2857142857142857, "#1bcfd4" ], [ 0.35714285714285715, "#24eca6" ], [ 0.42857142857142855, "#61fc6c" ], [ 0.5, "#a4fc3b" ], [ 0.5714285714285714, "#d1e834" ], [ 0.6428571428571429, "#f3c63a" ], [ 0.7142857142857143, "#fe9b2d" ], [ 0.7857142857142857, "#f36315" ], [ 0.8571428571428571, "#d93806" ], [ 0.9285714285714286, "#b11901" ], [ 1, "#7a0402" ] ], "showscale": false }, "height": 600, "legend": { "tracegroupgap": 0 }, "template": { "data": { "bar": [ { "error_x": { "color": "#2a3f5f" }, "error_y": { "color": "#2a3f5f" }, "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "bar" } ], "barpolar": [ { "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "barpolar" } ], "carpet": [ { "aaxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "baxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "type": "carpet" } ], "choropleth": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "choropleth" } ], "contour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "contour" } ], "contourcarpet": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "contourcarpet" } ], "heatmap": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmap" } ], "heatmapgl": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmapgl" } ], "histogram": [ { "marker": { "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "histogram" } ], "histogram2d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2d" } ], "histogram2dcontour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2dcontour" } ], "mesh3d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "mesh3d" } ], "parcoords": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "parcoords" } ], "pie": [ { "automargin": true, "type": "pie" } ], "scatter": [ { "fillpattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 }, "type": "scatter" } ], "scatter3d": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter3d" } ], "scattercarpet": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattercarpet" } ], "scattergeo": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergeo" } ], "scattergl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergl" } ], "scattermapbox": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattermapbox" } ], "scatterpolar": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolar" } ], "scatterpolargl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolargl" } ], "scatterternary": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterternary" } ], "surface": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "surface" } ], "table": [ { "cells": { "fill": { "color": "#EBF0F8" }, "line": { "color": "white" } }, "header": { "fill": { "color": "#C8D4E3" }, "line": { "color": "white" } }, "type": "table" } ] }, "layout": { "annotationdefaults": { "arrowcolor": "#2a3f5f", "arrowhead": 0, "arrowwidth": 1 }, "autotypenumbers": "strict", "coloraxis": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "colorscale": { "diverging": [ [ 0, "#8e0152" ], [ 0.1, "#c51b7d" ], [ 0.2, "#de77ae" ], [ 0.3, "#f1b6da" ], [ 0.4, "#fde0ef" ], [ 0.5, "#f7f7f7" ], [ 0.6, "#e6f5d0" ], [ 0.7, "#b8e186" ], [ 0.8, "#7fbc41" ], [ 0.9, "#4d9221" ], [ 1, "#276419" ] ], "sequential": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "sequentialminus": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ] }, "colorway": [ "#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880", "#FF97FF", "#FECB52" ], "font": { "color": "#2a3f5f" }, "geo": { "bgcolor": "white", "lakecolor": "white", "landcolor": "#E5ECF6", "showlakes": true, "showland": true, "subunitcolor": "white" }, "hoverlabel": { "align": "left" }, "hovermode": "closest", "mapbox": { "style": "light" }, "paper_bgcolor": "white", "plot_bgcolor": "#E5ECF6", "polar": { "angularaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "radialaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "scene": { "xaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "yaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "zaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" } }, "shapedefaults": { "line": { "color": "#2a3f5f" } }, "ternary": { "aaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "baxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "caxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "title": { "x": 0.05 }, "xaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 }, "yaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 } } }, "title": { "text": "Top 10 States with the Most Complaints" }, "width": 1000, "xaxis": { "anchor": "y", "domain": [ 0, 1 ], "tickangle": 0, "title": { "text": "State" } }, "yaxis": { "anchor": "x", "categoryarray": [ "Maryland", "New Jersey", "North Carolina", "Pennsylvania", "Illinois", "New York", "Georgia", "Texas", "California", "Florida" ], "categoryorder": "array", "domain": [ 0, 1 ], "tickfont": { "size": 10 }, "title": { "text": "Number of Complaints" } } } } }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import pandas as pd\n", "import plotly.express as px\n", "\n", "# State abbreviation to full name mapping\n", "state_mapping = {\n", " 'FL': 'Florida', 'CA': 'California', 'TX': 'Texas', 'GA': 'Georgia',\n", " 'NY': 'New York', 'IL': 'Illinois', 'PA': 'Pennsylvania', 'NC': 'North Carolina',\n", " 'NJ': 'New Jersey', 'MD': 'Maryland', 'VA': 'Virginia', 'OH': 'Ohio',\n", " 'MI': 'Michigan', 'SC': 'South Carolina', 'AZ': 'Arizona', 'TN': 'Tennessee',\n", " 'NV': 'Nevada', 'LA': 'Louisiana', 'AL': 'Alabama', 'MO': 'Missouri',\n", " 'MA': 'Massachusetts', 'IN': 'Indiana', 'AR': 'Arkansas', 'WA': 'Washington',\n", " 'CO': 'Colorado', 'MS': 'Mississippi', 'CT': 'Connecticut', 'MN': 'Minnesota',\n", " 'WI': 'Wisconsin', 'KY': 'Kentucky', 'UT': 'Utah', 'DE': 'Delaware',\n", " 'OR': 'Oregon', 'OK': 'Oklahoma', 'DC': 'District of Columbia', 'KS': 'Kansas',\n", " 'IA': 'Iowa', 'NM': 'New Mexico', 'NE': 'Nebraska', 'HI': 'Hawaii',\n", " 'RI': 'Rhode Island', 'ID': 'Idaho', 'WV': 'West Virginia', 'NH': 'New Hampshire',\n", " 'ME': 'Maine', 'MT': 'Montana', 'ND': 'North Dakota', 'AK': 'Alaska',\n", " 'SD': 'South Dakota', 'WY': 'Wyoming', 'VT': 'Vermont'\n", " # Removed territories and minor outlying islands not listed as states\n", "}\n", "\n", "# Assuming df_new is your DataFrame and 'State' contains the abbreviations\n", "# Map state abbreviations to full names\n", "df_new['State Name'] = df_new['State'].map(state_mapping)\n", "\n", "# Calculate complaint counts by state\n", "state_complaint_counts = df_new['State Name'].value_counts()\n", "\n", "# Get top 10 states with the most complaint counts\n", "top_n = 10\n", "top_states = state_complaint_counts.head(top_n)\n", "\n", "# Create a horizontal bar chart using Plotly Express with a nice color scale\n", "fig = px.bar(\n", " y=top_states.values,\n", " x=top_states.index,\n", " orientation='v',\n", " color=top_states.values, # Assign color based on values\n", " color_continuous_scale='Turbo', # A nice color scale\n", " title=f'Top {top_n} States with the Most Complaints',\n", " labels={'y': 'Number of Complaints', 'x': 'State'},\n", " category_orders={'y': top_states.index.tolist()}\n", ")\n", "\n", "fig.update_layout(\n", " yaxis=dict(\n", " tickfont=dict(size=10),\n", " ),\n", " xaxis=dict(\n", " tickangle=0,\n", " ),\n", " height=600,\n", " width=1000,\n", ")\n", "\n", "# To display a color bar, showing the mapping of colors to values\n", "fig.update_layout(coloraxis_showscale=False)\n", "\n", "fig.show()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6. Top 10 states with least complaints" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "config": { "plotlyServerURL": "https://plot.ly" }, "data": [ { "alignmentgroup": "True", "hovertemplate": "State=%{x}
Number of Complaints=%{y}
color=%{marker.color}", "legendgroup": "", "marker": { "color": [ 1309, 1041, 1040, 954, 795, 639, 530, 479, 404, 354 ], "coloraxis": "coloraxis", "pattern": { "shape": "" } }, "name": "", "offsetgroup": "", "orientation": "v", "showlegend": false, "textposition": "auto", "type": "bar", "x": [ "Idaho", "West Virginia", "New Hampshire", "Maine", "Montana", "North Dakota", "Alaska", "South Dakota", "Wyoming", "Vermont" ], "xaxis": "x", "y": [ 1309, 1041, 1040, 954, 795, 639, 530, 479, 404, 354 ], "yaxis": "y" } ], "layout": { "barmode": "relative", "coloraxis": { "colorbar": { "title": { "text": "color" } }, "colorscale": [ [ 0, "#30123b" ], [ 0.07142857142857142, "#4145ab" ], [ 0.14285714285714285, "#4675ed" ], [ 0.21428571428571427, "#39a2fc" ], [ 0.2857142857142857, "#1bcfd4" ], [ 0.35714285714285715, "#24eca6" ], [ 0.42857142857142855, "#61fc6c" ], [ 0.5, "#a4fc3b" ], [ 0.5714285714285714, "#d1e834" ], [ 0.6428571428571429, "#f3c63a" ], [ 0.7142857142857143, "#fe9b2d" ], [ 0.7857142857142857, "#f36315" ], [ 0.8571428571428571, "#d93806" ], [ 0.9285714285714286, "#b11901" ], [ 1, "#7a0402" ] ], "showscale": false }, "height": 600, "legend": { "tracegroupgap": 0 }, "template": { "data": { "bar": [ { "error_x": { "color": "#2a3f5f" }, "error_y": { "color": "#2a3f5f" }, "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "bar" } ], "barpolar": [ { "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "barpolar" } ], "carpet": [ { "aaxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "baxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "type": "carpet" } ], "choropleth": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "choropleth" } ], "contour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "contour" } ], "contourcarpet": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "contourcarpet" } ], "heatmap": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmap" } ], "heatmapgl": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmapgl" } ], "histogram": [ { "marker": { "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "histogram" } ], "histogram2d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2d" } ], "histogram2dcontour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2dcontour" } ], "mesh3d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "mesh3d" } ], "parcoords": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "parcoords" } ], "pie": [ { "automargin": true, "type": "pie" } ], "scatter": [ { "fillpattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 }, "type": "scatter" } ], "scatter3d": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter3d" } ], "scattercarpet": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattercarpet" } ], "scattergeo": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergeo" } ], "scattergl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergl" } ], "scattermapbox": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattermapbox" } ], "scatterpolar": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolar" } ], "scatterpolargl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolargl" } ], "scatterternary": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterternary" } ], "surface": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "surface" } ], "table": [ { "cells": { "fill": { "color": "#EBF0F8" }, "line": { "color": "white" } }, "header": { "fill": { "color": "#C8D4E3" }, "line": { "color": "white" } }, "type": "table" } ] }, "layout": { "annotationdefaults": { "arrowcolor": "#2a3f5f", "arrowhead": 0, "arrowwidth": 1 }, "autotypenumbers": "strict", "coloraxis": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "colorscale": { "diverging": [ [ 0, "#8e0152" ], [ 0.1, "#c51b7d" ], [ 0.2, "#de77ae" ], [ 0.3, "#f1b6da" ], [ 0.4, "#fde0ef" ], [ 0.5, "#f7f7f7" ], [ 0.6, "#e6f5d0" ], [ 0.7, "#b8e186" ], [ 0.8, "#7fbc41" ], [ 0.9, "#4d9221" ], [ 1, "#276419" ] ], "sequential": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "sequentialminus": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ] }, "colorway": [ "#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880", "#FF97FF", "#FECB52" ], "font": { "color": "#2a3f5f" }, "geo": { "bgcolor": "white", "lakecolor": "white", "landcolor": "#E5ECF6", "showlakes": true, "showland": true, "subunitcolor": "white" }, "hoverlabel": { "align": "left" }, "hovermode": "closest", "mapbox": { "style": "light" }, "paper_bgcolor": "white", "plot_bgcolor": "#E5ECF6", "polar": { "angularaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "radialaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "scene": { "xaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "yaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "zaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" } }, "shapedefaults": { "line": { "color": "#2a3f5f" } }, "ternary": { "aaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "baxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "caxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "title": { "x": 0.05 }, "xaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 }, "yaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 } } }, "title": { "text": "Top 10 States with the Most Complaints" }, "width": 1050, "xaxis": { "anchor": "y", "domain": [ 0, 1 ], "tickangle": 0, "title": { "text": "State" } }, "yaxis": { "anchor": "x", "categoryarray": [ "Vermont", "Wyoming", "South Dakota", "Alaska", "North Dakota", "Montana", "Maine", "New Hampshire", "West Virginia", "Idaho" ], "categoryorder": "array", "domain": [ 0, 1 ], "tickfont": { "size": 10 }, "title": { "text": "Number of Complaints" } } } } }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import pandas as pd\n", "import plotly.express as px\n", "\n", "# State abbreviation to full name mapping\n", "state_mapping = {\n", " 'FL': 'Florida', 'CA': 'California', 'TX': 'Texas', 'GA': 'Georgia',\n", " 'NY': 'New York', 'IL': 'Illinois', 'PA': 'Pennsylvania', 'NC': 'North Carolina',\n", " 'NJ': 'New Jersey', 'MD': 'Maryland', 'VA': 'Virginia', 'OH': 'Ohio',\n", " 'MI': 'Michigan', 'SC': 'South Carolina', 'AZ': 'Arizona', 'TN': 'Tennessee',\n", " 'NV': 'Nevada', 'LA': 'Louisiana', 'AL': 'Alabama', 'MO': 'Missouri',\n", " 'MA': 'Massachusetts', 'IN': 'Indiana', 'AR': 'Arkansas', 'WA': 'Washington',\n", " 'CO': 'Colorado', 'MS': 'Mississippi', 'CT': 'Connecticut', 'MN': 'Minnesota',\n", " 'WI': 'Wisconsin', 'KY': 'Kentucky', 'UT': 'Utah', 'DE': 'Delaware',\n", " 'OR': 'Oregon', 'OK': 'Oklahoma', 'DC': 'District of Columbia', 'KS': 'Kansas',\n", " 'IA': 'Iowa', 'NM': 'New Mexico', 'NE': 'Nebraska', 'HI': 'Hawaii',\n", " 'RI': 'Rhode Island', 'ID': 'Idaho', 'WV': 'West Virginia', 'NH': 'New Hampshire',\n", " 'ME': 'Maine', 'MT': 'Montana', 'ND': 'North Dakota', 'AK': 'Alaska',\n", " 'SD': 'South Dakota', 'WY': 'Wyoming', 'VT': 'Vermont'\n", " # Removed territories and minor outlying islands not listed as states\n", "}\n", "\n", "# Assuming df_new is your DataFrame and 'State' contains the abbreviations\n", "# Map state abbreviations to full names\n", "df_new['State Name'] = df_new['State'].map(state_mapping)\n", "\n", "# Calculate complaint counts by state\n", "state_complaint_counts = df_new['State Name'].value_counts()\n", "\n", "# Get top 10 states with the most complaint counts\n", "top_n = 10\n", "top_states = state_complaint_counts.tail(top_n)\n", "\n", "# Create a horizontal bar chart using Plotly Express with a nice color scale\n", "fig = px.bar(\n", " y=top_states.values,\n", " x=top_states.index,\n", " orientation='v',\n", " color=top_states.values, # Assign color based on values\n", " color_continuous_scale='Turbo', # A nice color scale\n", " title=f'Top {top_n} States with the Most Complaints',\n", " labels={'y': 'Number of Complaints', 'x': 'State'},\n", " category_orders={'y': top_states.index.tolist()}\n", ")\n", "\n", "fig.update_layout(\n", " yaxis=dict(\n", " tickfont=dict(size=10),\n", " ),\n", " xaxis=dict(\n", " tickangle=0,\n", " ),\n", " height=600,\n", " width=1050,\n", ")\n", "\n", "# To display a color bar, showing the mapping of colors to values\n", "fig.update_layout(coloraxis_showscale=False)\n", "\n", "fig.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6. Complaints across states" ] }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "config": { "plotlyServerURL": "https://plot.ly" }, "data": [ { "coloraxis": "coloraxis", "geo": "geo", "hovertemplate": "%{hovertext}

State=%{location}
Num_complaints=%{z}", "hovertext": [ "AA", "AE", "Alaska", "Alabama", "AP", "Arkansas", "AS", "Arizona", "California", "Colorado", "Connecticut", "District of Columbia", "Delaware", "Florida", "Georgia", "GU", "Hawaii", "Iowa", "Idaho", "Illinois", "Indiana", "Kansas", "Kentucky", "Louisiana", "Massachusetts", "Maryland", "Maine", "Michigan", "Minnesota", "Missouri", "Mississippi", "Montana", "North Carolina", "North Dakota", "Nebraska", "New Hampshire", "New Jersey", "New Mexico", "Nevada", "New York", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "PR", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "UNITED STATES MINOR OUTLYING ISLANDS", "Utah", "Virginia", "VI", "Vermont", "Washington", "Wisconsin", "West Virginia", "Wyoming" ], "locationmode": "USA-states", "locations": [ "AA", "AE", "AK", "AL", "AP", "AR", "AS", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "GU", "HI", "IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN", "MO", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR", "PA", "PR", "RI", "SC", "SD", "TN", "TX", "UNITED STATES MINOR OUTLYING ISLANDS", "UT", "VA", "VI", "VT", "WA", "WI", "WV", "WY" ], "name": "", "type": "choropleth", "z": [ 2, 48, 151, 4008, 15, 1856, 1, 4726, 27524, 1703, 2015, 1142, 1269, 36133, 20113, 13, 527, 763, 328, 13902, 3312, 666, 1522, 5309, 2938, 6751, 239, 6151, 1570, 3810, 2120, 193, 10696, 173, 819, 265, 9238, 646, 4454, 15619, 4958, 1088, 1019, 11707, 294, 422, 5271, 129, 4234, 31569, 14, 796, 6404, 33, 105, 1988, 1810, 286, 111 ] }, { "hoverinfo": "skip", "locationmode": "USA-states", "locations": [ "AA", "AE", "AK", "AL", "AP", "AR", "AS", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "GU", "HI", "IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN", "MO", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR", "PA", "PR", "RI", "SC", "SD", "TN", "TX", "UNITED STATES MINOR OUTLYING ISLANDS", "UT", "VA", "VI", "VT", "WA", "WI", "WV", "WY" ], "mode": "text", "text": [ "AA", "AE", "AK", "AL", "AP", "AR", "AS", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "GU", "HI", "IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN", "MO", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR", "PA", "PR", "RI", "SC", "SD", "TN", "TX", "UNITED STATES MINOR OUTLYING ISLANDS", "UT", "VA", "VI", "VT", "WA", "WI", "WV", "WY" ], "textfont": { "color": "white", "size": 8.5 }, "type": "scattergeo" } ], "layout": { "autosize": true, "coloraxis": { "colorbar": { "title": { "text": "Num_complaints" } }, "colorscale": [ [ 0, "#000004" ], [ 0.1111111111111111, "#1b0c41" ], [ 0.2222222222222222, "#4a0c6b" ], [ 0.3333333333333333, "#781c6d" ], [ 0.4444444444444444, "#a52c60" ], [ 0.5555555555555556, "#cf4446" ], [ 0.6666666666666666, "#ed6925" ], [ 0.7777777777777778, "#fb9b06" ], [ 0.8888888888888888, "#f7d13d" ], [ 1, "#fcffa4" ] ] }, "geo": { "bgcolor": "rgb(255, 255, 255)", "center": {}, "domain": { "x": [ 0, 1 ], "y": [ 0, 1 ] }, "lakecolor": "rgb(255, 255, 255)", "landcolor": "rgb(217, 217, 217)", "scope": "usa" }, "height": 400, "legend": { "tracegroupgap": 0 }, "margin": { "b": 0, "l": 0, "r": 0, "t": 50 }, "paper_bgcolor": "rgb(255, 255, 255)", "template": { "data": { "bar": [ { "error_x": { "color": "#2a3f5f" }, "error_y": { "color": "#2a3f5f" }, "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "bar" } ], "barpolar": [ { "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "barpolar" } ], "carpet": [ { "aaxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "baxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "type": "carpet" } ], "choropleth": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "choropleth" } ], "contour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "contour" } ], "contourcarpet": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "contourcarpet" } ], "heatmap": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmap" } ], "heatmapgl": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmapgl" } ], "histogram": [ { "marker": { "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "histogram" } ], "histogram2d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2d" } ], "histogram2dcontour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2dcontour" } ], "mesh3d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "mesh3d" } ], "parcoords": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "parcoords" } ], "pie": [ { "automargin": true, "type": "pie" } ], "scatter": [ { "fillpattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 }, "type": "scatter" } ], "scatter3d": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter3d" } ], "scattercarpet": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattercarpet" } ], "scattergeo": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergeo" } ], "scattergl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergl" } ], "scattermapbox": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattermapbox" } ], "scatterpolar": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolar" } ], "scatterpolargl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolargl" } ], "scatterternary": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterternary" } ], "surface": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "surface" } ], "table": [ { "cells": { "fill": { "color": "#EBF0F8" }, "line": { "color": "white" } }, "header": { "fill": { "color": "#C8D4E3" }, "line": { "color": "white" } }, "type": "table" } ] }, "layout": { "annotationdefaults": { "arrowcolor": "#2a3f5f", "arrowhead": 0, "arrowwidth": 1 }, "autotypenumbers": "strict", "coloraxis": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "colorscale": { "diverging": [ [ 0, "#8e0152" ], [ 0.1, "#c51b7d" ], [ 0.2, "#de77ae" ], [ 0.3, "#f1b6da" ], [ 0.4, "#fde0ef" ], [ 0.5, "#f7f7f7" ], [ 0.6, "#e6f5d0" ], [ 0.7, "#b8e186" ], [ 0.8, "#7fbc41" ], [ 0.9, "#4d9221" ], [ 1, "#276419" ] ], "sequential": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "sequentialminus": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ] }, "colorway": [ "#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880", "#FF97FF", "#FECB52" ], "font": { "color": "#2a3f5f" }, "geo": { "bgcolor": "white", "lakecolor": "white", "landcolor": "#E5ECF6", "showlakes": true, "showland": true, "subunitcolor": "white" }, "hoverlabel": { "align": "left" }, "hovermode": "closest", "mapbox": { "style": "light" }, "paper_bgcolor": "white", "plot_bgcolor": "#E5ECF6", "polar": { "angularaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "radialaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "scene": { "xaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "yaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "zaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" } }, "shapedefaults": { "line": { "color": "#2a3f5f" } }, "ternary": { "aaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "baxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "caxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "title": { "x": 0.05 }, "xaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 }, "yaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 } } }, "title": { "text": "Number of Complaints by State", "x": 0.45 }, "width": 1000 } } }, "metadata": {}, "output_type": "display_data" } ], "source": [ "df_2023 = df_new[df_new['Date received'].dt.year == 2023]\n", "\n", "state_complaints = df_2023.groupby('State').size().reset_index(name='Num_complaints')\n", "#state_complaints['text'] = 'Total Complaints in 2023: ' + state_complaints['Num_complaints'].astype(str)\n", "state_complaints['Full_state_name'] = state_complaints['State'].apply(lambda x : state_mapping[x] if x in state_mapping else x)\n", "\n", "fig = px.choropleth(state_complaints,\n", " locations='State',\n", " locationmode='USA-states',\n", " color='Num_complaints',\n", " color_continuous_scale='Inferno',\n", " scope=\"usa\",\n", " hover_name='Full_state_name', \n", " #hover_data={'State': False, 'Num_complaints': True}, \n", " title='Number of Complaints by State')\n", "fig.add_scattergeo(\n", " locations=state_complaints['State'], ###codes for states,\n", " locationmode='USA-states',\n", " text=state_complaints['State'],\n", " mode='text',\n", " hoverinfo='skip',\n", " textfont=dict(size = 8.5,color='white'))\n", "\n", "fig.update_layout(\n", " autosize = True,\n", " title_text='Number of Complaints by State',\n", " title_x=0.45,\n", " geo=dict(\n", " landcolor='rgb(217, 217, 217)', \n", " lakecolor='rgb(255, 255, 255)', \n", " bgcolor='rgb(255, 255, 255)' \n", " ),\n", " paper_bgcolor='rgb(255, 255, 255)', \n", " margin={\"r\":0,\"t\":50,\"l\":0,\"b\":0},\n", " width=1000,\n", " height=400\n", ")\n", "\n", "fig.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 7. Evolution of complaints across the years" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [ { "data": { "application/vnd.plotly.v1+json": { "config": { "plotlyServerURL": "https://plot.ly" }, "data": [ { "hovertemplate": "Year=%{x}
Number of Complaints=%{y}", "legendgroup": "", "line": { "color": "#636efa", "dash": "solid" }, "marker": { "symbol": "circle" }, "mode": "lines", "name": "", "orientation": "v", "showlegend": false, "type": "scatter", "x": [ "2015-03", "2015-04", "2015-05", "2015-06", "2015-07", "2015-08", "2015-09", "2015-10", "2015-11", "2015-12", "2016-01", "2016-02", "2016-03", "2016-04", "2016-05", "2016-06", "2016-07", "2016-08", "2016-09", "2016-10", "2016-11", "2016-12", "2017-01", "2017-02", "2017-03", "2017-04", "2017-05", "2017-06", "2017-07", "2017-08", "2017-09", "2017-10", "2017-11", "2017-12", "2018-01", "2018-02", "2018-03", "2018-04", "2018-05", "2018-06", "2018-07", "2018-08", "2018-09", "2018-10", "2018-11", "2018-12", "2019-01", "2019-02", "2019-03", "2019-04", "2019-05", "2019-06", "2019-07", "2019-08", "2019-09", "2019-10", "2019-11", "2019-12", "2020-01", "2020-02", "2020-03", "2020-04", "2020-05", "2020-06", "2020-07", "2020-08", "2020-09", "2020-10", "2020-11", "2020-12", "2021-01", "2021-02", "2021-03", "2021-04", "2021-05", "2021-06", "2021-07", "2021-08", "2021-09", "2021-10", "2021-11", "2021-12", "2022-01", "2022-02", "2022-03", "2022-04", "2022-05", "2022-06", "2022-07", "2022-08", "2022-09", "2022-10", "2022-11", "2022-12", "2023-01", "2023-02", "2023-03", "2023-04", "2023-05", "2023-06", "2023-07", "2023-08", "2023-09", "2023-10", "2023-11", "2023-12" ], "xaxis": "x", "y": [ 328, 671, 660, 740, 784, 747, 670, 598, 513, 621, 676, 705, 816, 761, 767, 762, 757, 930, 868, 824, 713, 849, 941, 944, 1039, 1879, 4018, 3780, 4251, 4393, 3860, 4037, 3844, 3790, 4727, 4503, 4658, 4498, 4324, 3578, 3906, 4186, 3450, 4177, 3729, 3590, 3420, 4063, 4321, 4211, 4424, 4643, 4858, 5399, 4422, 5126, 4516, 4365, 5178, 4990, 6058, 7216, 7925, 7487, 8342, 7452, 6826, 6824, 6539, 7735, 7306, 6039, 7154, 5918, 5695, 6421, 6875, 6330, 5955, 5893, 5914, 8267, 9056, 10608, 12004, 15980, 13279, 14289, 15818, 16887, 15630, 17345, 17117, 18921, 21995, 18916, 22179, 19771, 22543, 22499, 22580, 25923, 23732, 23773, 19615, 21442 ], "yaxis": "y" } ], "layout": { "height": 500, "legend": { "tracegroupgap": 0 }, "margin": { "t": 60 }, "template": { "data": { "bar": [ { "error_x": { "color": "#2a3f5f" }, "error_y": { "color": "#2a3f5f" }, "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "bar" } ], "barpolar": [ { "marker": { "line": { "color": "#E5ECF6", "width": 0.5 }, "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "barpolar" } ], "carpet": [ { "aaxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "baxis": { "endlinecolor": "#2a3f5f", "gridcolor": "white", "linecolor": "white", "minorgridcolor": "white", "startlinecolor": "#2a3f5f" }, "type": "carpet" } ], "choropleth": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "choropleth" } ], "contour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "contour" } ], "contourcarpet": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "contourcarpet" } ], "heatmap": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmap" } ], "heatmapgl": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "heatmapgl" } ], "histogram": [ { "marker": { "pattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 } }, "type": "histogram" } ], "histogram2d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2d" } ], "histogram2dcontour": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "histogram2dcontour" } ], "mesh3d": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "type": "mesh3d" } ], "parcoords": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "parcoords" } ], "pie": [ { "automargin": true, "type": "pie" } ], "scatter": [ { "fillpattern": { "fillmode": "overlay", "size": 10, "solidity": 0.2 }, "type": "scatter" } ], "scatter3d": [ { "line": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatter3d" } ], "scattercarpet": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattercarpet" } ], "scattergeo": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergeo" } ], "scattergl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattergl" } ], "scattermapbox": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scattermapbox" } ], "scatterpolar": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolar" } ], "scatterpolargl": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterpolargl" } ], "scatterternary": [ { "marker": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "type": "scatterternary" } ], "surface": [ { "colorbar": { "outlinewidth": 0, "ticks": "" }, "colorscale": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "type": "surface" } ], "table": [ { "cells": { "fill": { "color": "#EBF0F8" }, "line": { "color": "white" } }, "header": { "fill": { "color": "#C8D4E3" }, "line": { "color": "white" } }, "type": "table" } ] }, "layout": { "annotationdefaults": { "arrowcolor": "#2a3f5f", "arrowhead": 0, "arrowwidth": 1 }, "autotypenumbers": "strict", "coloraxis": { "colorbar": { "outlinewidth": 0, "ticks": "" } }, "colorscale": { "diverging": [ [ 0, "#8e0152" ], [ 0.1, "#c51b7d" ], [ 0.2, "#de77ae" ], [ 0.3, "#f1b6da" ], [ 0.4, "#fde0ef" ], [ 0.5, "#f7f7f7" ], [ 0.6, "#e6f5d0" ], [ 0.7, "#b8e186" ], [ 0.8, "#7fbc41" ], [ 0.9, "#4d9221" ], [ 1, "#276419" ] ], "sequential": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ], "sequentialminus": [ [ 0, "#0d0887" ], [ 0.1111111111111111, "#46039f" ], [ 0.2222222222222222, "#7201a8" ], [ 0.3333333333333333, "#9c179e" ], [ 0.4444444444444444, "#bd3786" ], [ 0.5555555555555556, "#d8576b" ], [ 0.6666666666666666, "#ed7953" ], [ 0.7777777777777778, "#fb9f3a" ], [ 0.8888888888888888, "#fdca26" ], [ 1, "#f0f921" ] ] }, "colorway": [ "#636efa", "#EF553B", "#00cc96", "#ab63fa", "#FFA15A", "#19d3f3", "#FF6692", "#B6E880", "#FF97FF", "#FECB52" ], "font": { "color": "#2a3f5f" }, "geo": { "bgcolor": "white", "lakecolor": "white", "landcolor": "#E5ECF6", "showlakes": true, "showland": true, "subunitcolor": "white" }, "hoverlabel": { "align": "left" }, "hovermode": "closest", "mapbox": { "style": "light" }, "paper_bgcolor": "white", "plot_bgcolor": "#E5ECF6", "polar": { "angularaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "radialaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "scene": { "xaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "yaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" }, "zaxis": { "backgroundcolor": "#E5ECF6", "gridcolor": "white", "gridwidth": 2, "linecolor": "white", "showbackground": true, "ticks": "", "zerolinecolor": "white" } }, "shapedefaults": { "line": { "color": "#2a3f5f" } }, "ternary": { "aaxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "baxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" }, "bgcolor": "#E5ECF6", "caxis": { "gridcolor": "white", "linecolor": "white", "ticks": "" } }, "title": { "x": 0.05 }, "xaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 }, "yaxis": { "automargin": true, "gridcolor": "white", "linecolor": "white", "ticks": "", "title": { "standoff": 15 }, "zerolinecolor": "white", "zerolinewidth": 2 } } }, "title": { "x": 0.5, "xanchor": "center", "y": 0.9, "yanchor": "top" }, "width": 1000, "xaxis": { "anchor": "y", "domain": [ 0, 1 ], "title": { "text": "Year" } }, "yaxis": { "anchor": "x", "domain": [ 0, 1 ], "title": { "text": "Number of Complaints" } } } } }, "metadata": {}, "output_type": "display_data" } ], "source": [ "monthly_complaints = df_new.copy()\n", "monthly_complaints = monthly_complaints[monthly_complaints['Date received'].dt.year != 2024]\n", "\n", "monthly_complaints['MonthYear'] = monthly_complaints['Date received'].dt.to_period('M').astype(str)\n", "monthly_complaints = monthly_complaints.groupby('MonthYear').size().reset_index(name = \"NumComplaints\")\n", "\n", "\n", "fig = px.line(monthly_complaints, x='MonthYear', y='NumComplaints',\n", " labels={'MonthYear': 'Year', 'NumComplaints': 'Number of Complaints'})\n", "\n", "fig.update_layout(\n", " title={\n", " 'y':0.9,\n", " 'x':0.5,\n", " 'xanchor': 'center',\n", " 'yanchor': 'top'\n", " },\n", " width=1000,\n", " height=500\n", " )\n", " \n", "fig.show()" ] } ], "metadata": { "kernelspec": { "display_name": "complaint-segmentation", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.19" } }, "nbformat": 4, "nbformat_minor": 2 }