Convert string column to float in Pandas

In this post, we are going to learn How to Convert string columns to float in Pandas that could be single or multiple columns using the built-in methods.

Methods to convert string column to float


  • astype(float) : for single or mutiple columns or entire dataframe
  • to_numeric() : for single or mutiple columns


1.Convert string column to float


The astype(float) method is very convenient when we have to convert any column values of the dataframe to another data type, even we can use Python dictionary to change multiple column datatype at a time,Where keys specify the column and values specify the new datatype.

The original dataframe we are using and its datatype

import pandas as pd
 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max'],
    'Marks':['100','100', '100'],
    'Subject': ['Math', 'Math', 'Music']
}
 


dfobj = pd.DataFrame(Student_dict)

print('original dataframe:\n',dfobj)

print(f"\n dataype of coluns:\n {dfobj.dtypes}")

Output

original dataframe:
    Name Marks Subject
0  Jack   100    Math
1  Rack   100    Math
2   Max   100   Music

 dataype of columns:
 Name       object
Marks      object
Subject    object
dtype: object

Program Example to Convert string column to float

import pandas as pd
 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max'],
    'Marks':['100','100', '100'],
    'Subject': ['Math', 'Math', 'Music']
}
 


dfobj = pd.DataFrame(Student_dict)


dfobj['Marks'] = dfobj['Marks'].astype(float)

print ('\n string to float :\n',dfobj)
print ('\n converted datatype :\n',dfobj.dtypes)

Output

 string to float :
    Name  Marks Subject
0  Jack  100.0    Math
1  Rack  100.0    Math
2   Max  100.0   Music

 converted datatype :
 Name        object
Marks      float64
Subject     object
dtype: object

2. to_numeric() to convert string to float


The to_numeric() function is used to convert given values to numeric values. We can use both numeric or non-numeric values.

The dataframe we are using and its datatype.

import pandas as pd
 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max'],
    'Marks':['100','100', 'z100'],
    'Subject': ['Math', 'Math', 'Music']
}
 


dfobj = pd.DataFrame(Student_dict)

print('original dataframe:\n',dfobj)

print(f"\n dataype of column:\n {dfobj.dtypes}")

Output

original dataframe:
    Name Marks Subject
0  Jack   100    Math
1  Rack   100    Math
2   Max  z100   Music

 dataype of coluns:
 Name       object
Marks      object
Subject    object
dtype: object

Program to convert string to float with to_numeric()

import pandas as pd
 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max'],
    'Marks':['100','100', 'z100'],
    'Subject': ['Math', 'Math', 'Music']
}
 


dfobj = pd.DataFrame(Student_dict)


dfobj['Marks'] = pd.to_numeric(dfobj['Marks'], errors='coerce')


print ('\n string to float :\n',dfobj)
print ('\n converted datatype :\n',dfobj.dtypes)

Output

 string to float :
    Name  Marks Subject
0  Jack  100.0    Math
1  Rack  100.0    Math
2   Max    NaN   Music

 converted datatype :
 Name        object
Marks      float64
Subject     object
dtype: object

3. Convert multiple columns to float


In this example, we are converting multiple columns that have a numeric string to float by using the astype(float) method of the pandas library.

We are using a Python dictionary to change multiple columns datatype Where keys specify the column and values specify a new datatype.

Program Example

import pandas as pd
 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max'],
    'Marks':['100','100', '100'],
    'Fee':['100','200','300'],
    'Subject': ['Math', 'Math', 'Music']
}
 


dfobj = pd.DataFrame(Student_dict)

dict_columns_type = {'Marks': float,
                'Fee': float
               }
  
dfobj = dfobj.astype(dict_columns_type)
print('dataframe str to float:\n',dfobj)

print(f'\n {dfobj.dtypes}')

Output

dataframe str to float:
    Name  Marks    Fee Subject
0  Jack  100.0  100.0    Math
1  Rack  100.0  200.0    Math
2   Max  100.0  300.0   Music

 Name        object
Marks      float64
Fee        float64
Subject     object
dtype: object

4. Convert multiple columns types to numeric


In this example, we are using apply() method and passing datatype to to_numeric as an argument to change columns numeric string value to integer.

Program Example

import pandas as pd
 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max'],
    'Marks':['100','100', '100'],
    'Fee':['100','200','300'],
    'Subject': ['Math', 'Math', 'Music']
}
 


dfobj = pd.DataFrame(Student_dict)

dfobj[['Marks','Fee']]= dfobj[['Marks','Fee']].apply(pd.to_numeric)
  

print('dataframe str to float:\n',dfobj)

print(f'\n {dfobj.dtypes}')

Output

dataframe str to float:
    Name  Marks  Fee Subject
0  Jack    100  100    Math
1  Rack    100  200    Math
2   Max    100  300   Music

 Name       object
Marks       int64
Fee         int64
Subject    object
dtype: object

5. Convert entire dataframe to float


To convert an entire dataframe columns to float we just need to call the astype() method using dataframe object

Program Example

import pandas as pd
 
Student_dict = {
    'StudID': ['12', '13', '14'],    
    'Marks':['100','100', '100'],
    'Fee':['100','200','300']
    
}
 


dfobj = pd.DataFrame(Student_dict)

dfobj= dfobj.astype(float)
  

print('dataframe str to float:\n',dfobj)

print(f'\n {dfobj.dtypes}')



Output

dataframe str to int:
    StudID  Marks    Fee
0    12.0  100.0  100.0
1    13.0  100.0  200.0
2    14.0  100.0  300.0

 StudID    float64
Marks     float64
Fee       float64
dtype: object

Summary

In this post we have understood different ways to Convert string columns to float in Pandas.