将int执行Apply(str)会创建一堆\n字符吗?

huangapple go评论58阅读模式
英文:

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:

将int执行Apply(str)会创建一堆\n字符吗?

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)

将int执行Apply(str)会创建一堆\n字符吗?

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)

huangapple
  • 本文由 发表于 2023年2月10日 06:13:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404998.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定