在包含大约 40 多列的数据框中,我尝试使用 iloc 将前 27 列的 dtype 从 float 更改为 int:
df1.iloc[:,0:27]=df1.iloc[:,0:27].astype('int')
Run Code Online (Sandbox Code Playgroud)
但是,它不起作用。我没有收到任何错误,但 dtype 也没有改变。它仍然保持浮动。
现在最奇怪的部分:
如果我首先仅更改第一列的 dtype(如下所示):
df1.iloc[:,0]=df1.iloc[:,0].astype('int')
Run Code Online (Sandbox Code Playgroud)
然后运行前面的代码行:
df1.iloc[:,0:27]=df1.iloc[:,0:27].astype('int')
Run Code Online (Sandbox Code Playgroud)
它按要求工作。任何帮助理解这一点和解决方案的帮助将不胜感激。
谢谢!