【问题标题】:Inverse Label Encoding for plotting purposes用于绘图的反向标签编码
【发布时间】:2019-09-22 07:00:53
【问题描述】:

我在用整数编码的 DataFrame 中有一个特性,即 int64 类型。此外,我有一本具有等效类的字典。有没有办法用原始类别为这个特征绘制条形图?

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

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

我想获得一个饼图,其中包含每个案例的相对频率(表示为 %),字符串标签包含在字典中。

【问题讨论】:

  • 请提供您的数据摘录和您想要获得的结果。

标签: python pandas


【解决方案1】:

这应该适合你。只需操作 df 来替换您想要的单词的值,然后获取计数。

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')

【讨论】:

    猜你喜欢
    • 2018-04-14
    • 2019-09-09
    • 2020-11-30
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多