将对象转换为Int pandas [英] Converting object to Int pandas

查看:105
本文介绍了将对象转换为Int pandas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我遇到了将对象的列转换为完整列的整数的问题.

Hello I have an issue to convert column of object to integer for complete column.

我有一个数据框,我尝试将一些检测为Object的列转换为Integer(或Float),但是我已经找到的所有答案都对我有用:(

I have a data frame and I tried to convert some columns that are detected as Object into Integer (or Float) but all the answers I already found are working for me :(

第一状态

然后我尝试应用to_numeric方法,但是不起作用. 数值方法

Then I tried to apply the to_numeric method but doesn't work. To numeric method

然后是一个自定义方法,您可以在这里找到: Pandas:convert dtype'object'诠释 但也不起作用:data3['Title'].astype(str).astype(int) (很抱歉,我无法再传递图像-您必须相信我,它不起作用)

Then a custom method that you can find here : Pandas: convert dtype 'object' to int but doesn't work either : data3['Title'].astype(str).astype(int) (Sorry I cannot pass the image anymore - you have to trust me that it doesn't work)

我尝试使用inplace语句,但似乎没有集成到这些方法中:

I tried to use the inplace statement but doesn't seem to be integrated in those methods :

我很确定答案是愚蠢的,但找不到它:( 有人可以帮助我吗?

I am pretty sure that the answer is dumb but cannot find it :( Is anyone able to help me ?

谢谢

推荐答案

您需要将输出分配回去:

You need assign output back:

#maybe also works omit astype(str)
data3['Title'] = data3['Title'].astype(str).astype(int)

或者:

data3['Title'] = pd.to_numeric(data3['Title'])

示例:

data3 = pd.DataFrame({'Title':['15','12','10']})
print (data3)
  Title
0    15
1    12
2    10

print (data3.dtypes)
Title    object
dtype: object


data3['Title'] = pd.to_numeric(data3['Title'])
print (data3.dtypes)
Title    int64
dtype: object


data3['Title'] = data3['Title'].astype(int)

print (data3.dtypes)
Title    int32
dtype: object

这篇关于将对象转换为Int pandas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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