python seaborn 未使用的类别名称
Category names not used by python seaborn
我正在尝试 countplot
使用有意义的类别名称。数据集使用整数作为类别代码,这些整数值显示在图中而不是我分配的名称。
import pandas
# bug fix
pandas.set_option('display.float_format', lambda x:'%f'%x)
import seaborn
import matplotlib.pyplot
s = pandas.Series([1,2,3,1,2,3,1])
print(s)
s = s.astype('category')
print(s)
s.cat.rename_categories(["A", "B", "C"])
print(s)
seaborn.countplot(x = s)
此代码生成原始类别值为 1、2 和 3 的图。我想要 A、B 和 C。
print(s)
输出是:
0 1
1 2
2 3
3 1
4 2
5 3
6 1
dtype: int64
0 1
1 2
2 3
3 1
4 2
5 3
6 1
dtype: category
Categories (3, int64): [1, 2, 3]
0 1
1 2
2 3
3 1
4 2
5 3
6 1
dtype: category
Categories (3, int64): [1, 2, 3]
所以它改变了数据类型但没有改变值。但是,当我仅以交互方式重命名时,我得到以下结果,尽管 print(s)
仍将 return 号码名称。
In[108]: s.cat.rename_categories(["A", "B", "C"])
Out[108]:
0 A
1 B
2 C
3 A
4 B
5 C
6 A
dtype: category
Categories (3, object): [A, B, C]
如何让绘图使用字母而不是数字?
s.cat.rename_categories(["A", "B", "C"])
(与大多数 pandas 操作一样,除非它们接受 inplace=True
选项)生成一个 new 对象。它不会更改您已有的 s
,因此您实际上根本没有分配任何新名称。您还需要分配结果:
>>> s = s.cat.rename_categories(["A", "B", "C"])
>>> s
0 A
1 B
2 C
3 A
4 B
5 C
6 A
dtype: category
Categories (3, object): [A, B, C]
>>> seaborn.countplot(x=s)
给我
我正在尝试 countplot
使用有意义的类别名称。数据集使用整数作为类别代码,这些整数值显示在图中而不是我分配的名称。
import pandas
# bug fix
pandas.set_option('display.float_format', lambda x:'%f'%x)
import seaborn
import matplotlib.pyplot
s = pandas.Series([1,2,3,1,2,3,1])
print(s)
s = s.astype('category')
print(s)
s.cat.rename_categories(["A", "B", "C"])
print(s)
seaborn.countplot(x = s)
此代码生成原始类别值为 1、2 和 3 的图。我想要 A、B 和 C。
print(s)
输出是:
0 1
1 2
2 3
3 1
4 2
5 3
6 1
dtype: int64
0 1
1 2
2 3
3 1
4 2
5 3
6 1
dtype: category
Categories (3, int64): [1, 2, 3]
0 1
1 2
2 3
3 1
4 2
5 3
6 1
dtype: category
Categories (3, int64): [1, 2, 3]
所以它改变了数据类型但没有改变值。但是,当我仅以交互方式重命名时,我得到以下结果,尽管 print(s)
仍将 return 号码名称。
In[108]: s.cat.rename_categories(["A", "B", "C"])
Out[108]:
0 A
1 B
2 C
3 A
4 B
5 C
6 A
dtype: category
Categories (3, object): [A, B, C]
如何让绘图使用字母而不是数字?
s.cat.rename_categories(["A", "B", "C"])
(与大多数 pandas 操作一样,除非它们接受 inplace=True
选项)生成一个 new 对象。它不会更改您已有的 s
,因此您实际上根本没有分配任何新名称。您还需要分配结果:
>>> s = s.cat.rename_categories(["A", "B", "C"])
>>> s
0 A
1 B
2 C
3 A
4 B
5 C
6 A
dtype: category
Categories (3, object): [A, B, C]
>>> seaborn.countplot(x=s)
给我