Converting object to Int pandas

pandas, python, python-3.x

Solution

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

Problem

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 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)` ( I cannot pass the image anymore - You have to trust me that it doesn't work) 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

Original source

Related problems