simon
simon

Reputation: 690

Inverse Label Encoding for plotting purposes

I have a feature in a DataFrame encoded with integers, i.e., int64 type. Further, I have a dictionary with the equivalent classes. Is there a way to draw a bar plot for this feature with the original categories?

df = pd.DataFrame({"feature_1": [-1, 0, 1, 1, 0, 1]})

tags = {"Good": 1, "Bad": 0, "Indiferent":-1}

I would like to obtain a pie chart with relative frequency (expressed %) of every case, with the string labels contained in the dictionary.

Upvotes: 0

Views: 178

Answers (1)

Ben Pap
Ben Pap

Reputation: 2579

This should work for you. Just have to manipulate the df to replace the values for the words you want, then get the count.

import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame({"feature_1": [-1, 0, 1, 1, 0, 1]})
tags = {"Good": 1, "Bad": 0, "Indiferent":-1}


df = df['feature_1'].replace({v:k for k,v in tags.items()})\
                    .value_counts()\
                    .reset_index()\
                    .rename(columns = {'index': 'feature_1','feature_1':'count'})

sns.catplot(x = "feature_1", y = 'count', data = df, kind = 'bar')

enter image description here

Upvotes: 1

Related Questions