英文:
Doing Apply(str) to int creates a bunch of \n's?
问题
我有一个类似这样按年份标记的数据框:
我想把这些ints
更改为strs
,因为它们实际上是分类变量。然而,这是结果:
X_train['YrSold'] = X_train['YrSold'].apply(str)
我更希望在.apply(str)
过程中自动去掉所有这些\n
,而不是通过正则表达式后处理每一列。似乎后者会更容易出错。
英文:
I have a dataframe like this labeled by year:
I would like to change these ints
to strs
because they are actually categorical variables. However, this is the result:
X_train['YrSold'] = X_train['YrSold'].apply(str)
I would prefer to get rid of all those \n
's automatically in the .apply(str)
process, as opposed to postprocessing each column via regex. Seems like the latter would have more room for error.
答案1
得分: 0
Solution was astype(str)
instead of apply(str)
.
解决方案是使用astype(str)
而不是apply(str)
。
英文:
Solution was astype(str)
instead of apply(str)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论