gpt4 book ai didi

python - 将数据框中某些列的 Float 转换为 Int

转载 作者:太空狗 更新时间:2023-10-29 21:30:46 25 4
gpt4 key购买 nike

我正在尝试将第 0 列转换为第 4 列,并将第 6 列从当前的浮点类型转换为整数。

我试过:

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

但这当然行不通...

最佳答案

考虑df

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

enter image description here

使用np.r_获取slc

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

或者传递一个以键作为列名的类型字典

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

enter image description here

关于python - 将数据框中某些列的 Float 转换为 Int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40855969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com