Convert int to Float in Python using Pandas
Introduction
We have already learnt about how to convert float variables to int. Now let us know how to convert int variables to float
So let us create another Dataset on which we can perform a few operations.
Code:
'''
1.) downaload pandas if unavailable
2.)Open Python idle window
3.) import pandas library
'''
import pandas as pd
# Created a Dictionary for creating a DataFrame
DataSet={
'players':['root','pujara','rohit'],
'SR':[60.71,32.43,42.49],
'Avg':[94.00,36.97,52.57],
'runs':[564,227,368]
}
# DataFrame Creation
df=pd.DataFrame(DataSet)
print(df)
Output:
players SR Avg runs
0 root 60.71 94.00 564
1 pujara 32.43 36.97 227
2 rohit 42.49 52.57 368
So as usual the way of converting a variable using explicit type conversion is not used. This type of conversion cannot be used as DataFrame is in read only format. There are two methods which can be used.
They are:
- astype(Data_Type)
- to_numeric()
astype() Method
This method helps us in changing the datatype of int to float directly in the DataFrame. astype() is inbuilt method which is present in the pandas library. This inbuilt method helps us to convert float to int, int to float etc. This method is present in DataFrame directly. We can use dot(.) operator to use this kind of method
Syntax:
DataFrame_name[‘column_name’]=DataFrame_name[‘column_name’].astype(Data_Type)
Code:
print(df)
print(df.dtypes)
df['runs']=df['runs'].astype(float)
print(df.dtypes
Output:
players SR Avg runs
0 root 60.71 94.00 564
1 pujara 32.43 36.97 227
2 rohit 42.49 52.57 368
players object
SR float64
Avg float64
runs int64
dtype: object
#After Conversion
players object
SR float64
Avg float64
runs float64
dtype: object
This is the way how a variable in DataFrame can be converted from int to float.
How to Perform Multiple data_types Conversions in a DataFrame?
Yes, we can convert multiple data_types in a DataFrame by using astype() method. Just there is a small change in syntax for astype() method. First let us know the syntax of this type of datatype conversion.
Syntax:
DataFrame_name=DataFram_name.astype({‘column_name’ :’data_type1’,’column_name’: ‘data_type2’……})
Let us explain this process with an example.
Example:
print(df.dtypes)
print(df)
df=df.astype({'SR':'int','runs':'int'})
print(df.dtypes)
Output:
players object
SR float64
Avg float64
runs int64
dtype: object
players SR Avg runs
0 root 60.71 94.00 564
1 pujara 32.43 36.97 227
2 rohit 42.49 52.57 368
players object
SR int32
Avg float64
runs int32
dtype: object
to_numeric()
This is a method which is already in-built in Python library. to_numeric is also is used to change the data_type of a column. To use this method, we need to know the syntax of the to_numeric method.
Syntax:
pandas.to_numeric(DataFrame[‘column_name’],
Example:
df=pd.DataFrame(DataSet)
print(df.dtypes)
df['Avg']=pd.to_numeric(df['Avg'],downcast='int')
print(df.dtypes)
Output:
players object
SR float64
Avg int64
runs int64
dtype: object
players object
SR float64
Avg float32
runs int64
dtype: object
This is how we convert int to float and float to int