Last active
May 4, 2020 15:09
-
-
Save Ze1598/b10c96b0617fc4f5d2c218d8341cc0c2 to your computer and use it in GitHub Desktop.
Handle Categorical data with pandas (str)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| import numpy as np | |
| import plotly.express as px | |
| from typing import List, Union | |
| def create_evaluations( | |
| categories: List[str], | |
| mapping_values: List[str], | |
| length: int | |
| ) -> pd.Categorical: | |
| evaluations = pd.Categorical( | |
| np.random.choice(categories, (length,)), | |
| categories=categories | |
| ) | |
| # The values are sorted from "terrible" to "excellent", just like in the\ | |
| # `options` list | |
| evaluations.sort_values(inplace=True) | |
| # Map the categorical nominal values to categorical numerical values | |
| evaluations = evaluations.map({ | |
| category: map_value for category, map_value in zip(categories, mapping_values) | |
| }) | |
| return evaluations | |
| def plot_column_chart( | |
| data_series: pd.Categorical, | |
| x_labels: List[str], | |
| title: str, | |
| axes_titles: List[Union[str, None]] | |
| ) -> None: | |
| # Change the categories' names to the desired X axis labels | |
| data_series = data_series.map({ | |
| series_value: x_label for series_value, x_label in zip(data_series.unique(), x_labels) | |
| }) | |
| fig = px.bar( | |
| x=data_series.unique(), | |
| y=data_series.value_counts(), | |
| title=title | |
| ) | |
| fig.update_layout( | |
| xaxis_title=axes_titles[0], | |
| yaxis_title=axes_titles[1] | |
| ) | |
| fig.update_layout( | |
| uniformtext_minsize=14, | |
| uniformtext_mode="hide", | |
| title_x=0.5 | |
| ) | |
| fig.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment