将数据框中某些列上的Float转换为Int [英] Converting Float to Int on certain columns in a data frame

查看:42
本文介绍了将数据框中某些列上的Float转换为Int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将第0列,第4列和第6列从当前的float类型转换为int.

I am trying to convert columns 0 to 4 and 6 to ints from there current float types.

我尝试过:

df[0:4,6].astype(int)

但是这当然行不通...

but of course this does not work...

推荐答案

考虑df

df = pd.DataFrame(np.random.rand(10, 10) * 10)

使用np.r_获取slc

slc = np.r_[0:4, 6]
df[slc] = df[slc].astype(int)
df

或传递以键为列名的类型的字典

or pass a dictionary of types with keys as column names

df.astype({c: int for c in slc})

这篇关于将数据框中某些列上的Float转换为Int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆