小编Ris*_*hik的帖子

我需要更改熊猫数据框中几列的类型。不能使用 iloc 这样做

在包含大约 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)

它按要求工作。任何帮助理解这一点和解决方案的帮助将不胜感激。

谢谢!

python types casting dataframe pandas

9
推荐指数
1
解决办法
340
查看次数

标签 统计

casting ×1

dataframe ×1

pandas ×1

python ×1

types ×1