Sometimes we have string numeric float values in the dataframe. In this post, we are going to learn in Pandas convert multiple columns to float with example by using the built-in method
Methods to convert mutiple column to float
- astype(float)
- to_numeric()
Pandas astype() method
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 columns datatypes at a time, Where keys specify the column and values specify the new datatype.
1. Pandas 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 panda’s library.
We are 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
2. Convert multiple columns type to numeric
In this example, we are using apply() method and passing datatype to_numeric as an argument to change columns numeric string value to an 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
3. Convert entire dataframe to float
To convert an entire dataframe column to float we just need to call the astype(float) method using the 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
4. Dataframe.Inter_objects()
“Inter_objects() is a soft conversion of object-dtyped columns, leaving non-object and unconvertible columns unchanged”.
Program Example
import pandas as pd
import numpy as np
Student_dict = {
'Name': ['Jack', 'Rack', 'Max'],
'Marks':[100,100,100],
'Subject': ['Math', 'Math', 'Music']
}
dfobj = pd.DataFrame(Student_dict,dtype ='object')
dfobj = dfobj.infer_objects()
print(dfobj.dtypes)
Output
Name object
Marks int64
Subject object
dtype: object
Summary
In this post, we have understood different multiple ways in Pandas how to convert multiple columns to float.