1

I have written a small code to test how to change datatype of a column of a DataFrame in Python (Version 3.8.5 and running the code in Jupyter Notebook). But it is not changing the datatype. Any idea, what I am doing wrong?

import pandas as pd

data = {'Name':['Tom', 'nick', 'krish', 'jack'],
        'Age':['20+', '21.5', '19+', '18.6']}

df = pd.DataFrame(data)

df["Age_Corrected"] = df["Age"].str.replace("+","")
df
df["Age_Corrected"].astype("float")
df.info()

But I am not getting Age_Corrected as float. It is still showing object. The result I am getting from df.info() is given below.

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 3 columns):
 #   Column         Non-Null Count  Dtype 
---  ------         --------------  ----- 
 0   Name           4 non-null      object
 1   Age            4 non-null      object
 2   Age_Corrected  4 non-null      object
dtypes: object(3)
memory usage: 112.0+ bytes
Mr Learner
  • 63
  • 9
  • 3
    Check out the `astype` section of the accepted answer of the linked duplicate. As the answers below notes, `astype` is not an in-place operation. – Henry Ecker Jun 21 '21 at 17:52

2 Answers2

4

You have to do

df["Age_Corrected"] = df["Age_Corrected"].astype("float")

Rafael Neves
  • 467
  • 3
  • 10
1

It does not change because you don't reaffect the column. Try this one

df["Age_Corrected"] = df["Age_Corrected"].astype("float")