我有一个包含几个字符串值的Pandas数据帧。 为了计算相似度,我想用整数值替换它们。 例如:
stores[['CNPJ_Store_Code','region','total_facings']].head()
Out[24]:
CNPJ_Store_Code region total_facings
1 93209765046613 Geo RS/SC 1.471690
16 93209765046290 Geo RS/SC 1.385636
19 93209765044084 Geo PR/SPI 0.217054
21 93209765044831 Geo RS/SC 0.804633
23 93209765045218 Geo PR/SPI 0.708165
我想替换region=='Geo RS/SC'==>;1,region=='Geo PR/SPI'===>;2等等
Clarification: I want to do the replacement automatically, without creating a dictionary first, since I don't know in advance what my regions will be. Any ideas? I am trying to use DictVectorizer, with no success.
我肯定有一种聪明的方法可以做到,但我就是找不到。在
有人知道解决方案吗?在
在我看来你真的很喜欢熊猫类
http://pandas-docs.github.io/pandas-docs-travis/categorical.html
我想您只需要将文本列的数据类型改为“category”就可以了。在
您可以:
您可以使用
.apply()
函数和字典将所有已知字符串值映射到其相应的整数值:相关问题 更多 >
编程相关推荐