将对象转换为 Int Pandas

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43677933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:30:09  来源:igfitidea点击:

Converting object to Int pandas

python-3.xpandas

提问by Pitchkrak

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

您好,我有一个问题,将对象列转换为整列的整数。

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 :(

我有一个数据框,我试图将一些被检测为对象的列转换为整数(或浮点数),但我已经找到的所有答案都对我有用:(

First status

第一状态

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

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

Then a custom method that you can find here : Pandas: convert dtype 'object' to intbut 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)

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

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

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

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

我很确定答案很愚蠢,但找不到:(有人能帮我吗?

Thanks

谢谢

回答by jezrael

You need assign output back:

您需要重新分配输出:

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

Or:

或者:

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

Sample:

样本:

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

回答by ArwaFahad

also you can try this code, work fine with me

你也可以试试这个代码,和我一起工作

data3.Title= pd.factorize(data3.Title)[0]

回答by python_enthusiast

2 years and 11 months later, but here I go.

2 年零 11 个月后,但我走了。

It's important to check if your data has any spaces, special characters (like commas, dots, or whatever else) first. If yes, then you need to basically remove those and then convert your string data into float and then into an integer (this is what worked for me for the case where my data was numerical values but with commas, like 4,118,662).

首先检查您的数据是否有任何空格、特殊字符(如逗号、点或其他任何内容)非常重要。如果是,那么您需要基本上删除它们,然后将您的字符串数据转换为浮点数,然后再转换为整数(这对我的数据是数值但带有逗号的情况下有用,例如 4,118,662)。

data3.Title = data3.Title.str.replace(',', '').astype(flaoat).astype(int)

回答by dt170

As python_enthusiast said ,

正如python_狂热者所说,

This command works for me too

这个命令也适用于我

data3.Title = data3.Title.str.replace(',', '').astype(float).astype(int)

but also works fine with

但也适用于

data3.Title = data3.Title.str.replace(',', '').astype(int)

you have to use str before replace in order to get rid of commas only then change it to int/float other wise you will get error .

你必须在替换之前使用 str 才能摆脱逗号,然后才将其更改为 int/float 否则你会得到错误。