擅长:python、mysql、java
<p>您需要为不同的项创建分类器映射的dict,例如</p>
<pre><code>classifier = {'oak': 'Tree',
'maple': 'Tree',
'holly': 'Tree',
'sawgrass': 'Grass',
'cat tails': 'Bush',
'birch': 'Tree'}
</code></pre>
<p>然后获取一列组就很简单了,只需对列调用<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.map.html" rel="nofollow noreferrer">^{<cd1>}</a>。你知道吗</p>
<pre><code>>>> df.species.map(classifier)
0 Tree
1 Tree
2 Tree
3 Grass
4 Bush
5 Tree
Name: species, dtype: object
</code></pre>
<p>所以你可以用</p>
<pre><code>df['classification'] = df.species.map(classifier)
</code></pre>