【问题标题】:df type not permanently changing to int [duplicate]df 类型不会永久更改为 int [重复]
【发布时间】:2020-08-13 19:29:47
【问题描述】:

很抱歉再次提问,但这对我来说没有意义

我正在尝试将列的数据类型更改为 int 并执行以下操作:

df.Age.astype(object).astype(int)

这可行,看起来它已更改为 int 但如果我这样做了

df.Age 

然后它作为对象数据类型出现?如何使更改永久化?

【问题讨论】:

  • 为什么是astype(object).astype(int)?为什么不直接.astype(int)
  • @DeepSpace astype(object).astype 是我在尝试解决它时一直在阅读的内容
  • @drec4s 是的,感谢 briba 的回答,我现在可以看到哪里出错了

标签: python jupyter


【解决方案1】:

你必须与意甲分配:

df.Age = df.Age.astype(int)

同时考虑:

df["Age"] = pd.to_numeric(df["Age"])

参考:Change data type of columns in Pandas

【讨论】:

  • 谢谢你让它工作了,而且aystype方式是我一直在网上阅读的。从未真正见过 to_numeric 方式
猜你喜欢
  • 1970-01-01
  • 2011-12-31
  • 1970-01-01
  • 2017-02-27
  • 2012-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多