擅长:python、mysql、java
<p>不再需要使用<code>map</code>。自版本0.15以来,Pandas允许其列使用分类数据类型。
存储的数据占用更少的空间,对它的操作更快,并且您可以使用标签</p>
<p>我以<a href="http://pandas.pydata.org/pandas-docs/stable/10min.html#categoricals" rel="noreferrer">pandas docs</a>为例:</p>
<pre><code>df = pd.DataFrame({"id":[1,2,3,4,5,6], "raw_grade":['a', 'b', 'b', 'a', 'a', 'e']})
#Recast grade as a categorical variable
df["grade"] = df["raw_grade"].astype("category")
df["grade"]
#Gives this:
Out[124]:
0 a
1 b
2 b
3 a
4 a
5 e
Name: grade, dtype: category
Categories (3, object): [a, b, e]
</code></pre>
<p>您还可以重命名类别和添加缺少的类别</p>