convert column to float pandas | astype pandas float
In this pandas tutorial, we will discuss about:
-
print data type in pandas,
-
convert column to float pandas,
-
astype pandas float
Lets first go through the concept of dataframe.
DataFrame in pandas is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.
We can able to create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.
Syntax:
pandas.DataFrame(data)
Where, data is the input dataframe, The data can be a dictionary that stores list of values with specified key.
Example: Create and print type of dataframe
In this print data type in pandas example, we will create a dataframe with 4 rows and 4 columns with college data and assign indices through index parameter.
We can get the dataframe datatypes using dtypes method.
Syntax:
dataframe.dtypes
In the below code we create and print data type in pandas dataframe.
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":['1200','3422','5644','670']
},index=['one','two','three','four'])
#display the dataframe
print(data)
# display the datatypes
print(data.dtypes)
Output: print data type in pandas result is below
college_id college_name college_address Total Staff
one c-001 vignan university guntur 1200
two c-021 vvit guntur 3422
three c-002 RVR - JC guntur 5644
four c-004 Andhra University guntur 670
college_id object
college_name object
college_address object
Total Staff object
dtype: object
Now we will use above dataframe to convert column to float in pandas.
Convert column to float in pandas
Lets see different methods to convert column to float in pandas.
Method 1 : convert column to float in pandas using astype()
This astype pandas method will take dataframe column as input and convert the data type to float.
We can convert to float by specifying a keyword called 'float'.
Syntax:
dataframe['column'].astype(float)
where,
1. dataframe is the input dataframe
2. column is the name of the column in which the datatype to be converted.
Note - In pandas DataFrame, the string type column will be considered as an object.
Example: convert column to float in pandas example
In this convert column to float in pandas example, we will convert Total Staff column to float type
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":['1200','3422','5644','670']
},index=['one','two','three','four'])
# convert Total Staff column to float type
data['Total Staff']=data['Total Staff'].astype(float)
# display the datatypes
print(data.dtypes)
# display the dataframe
print(data)
Output: convert column to float in pandas for column total staff
college_id object
college_name object
college_address object
Total Staff float64
dtype: object
college_id college_name college_address Total Staff
one c-001 vignan university guntur 1200.0
two c-021 vvit guntur 3422.0
three c-002 RVR - JC guntur 5644.0
four c-004 Andhra University guntur 670.0
From the output we observed that data type of Total Staff column is object before and now it is converted to Float type and we displayed the entire dataframe.
Method 2 : convert column to float pandas using astype() with dictionary
This astype pandas method with dictionary will take dataframe column as input and convert the data type to float.
We can convert to float by specifying a keyword called 'float'.
astype() will take column name inside a dictionary, such that key will be the column name and value will be the float keyword.
Syntax:
dataframe.astype({"column":float})
where,
1. dataframe is the input dataframe
2. column is the name of the column in which the datatype to be converted.
Note - In pandas DataFrame, the string type column will be considered as an object.
Example: convert column to float in pandas example
In this convert column to float in pandas example, we will convert Total Staff column to float type
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":['1200','3422','5644','670']
},index=['one','two','three','four'])
# convert Total Staff column to float type
data = data.astype({"Total Staff": float})
# display the datatypes
print(data.dtypes)
Output: result for convert column to float in pandas on total staff column is given below
college_id object
college_name object
college_address object
Total Staff float64
dtype: object
From the output we observed that data type of Total Staff column is object before and now it is converted to Float type.
Method 3 : convert column to float pandas using astype() by specifying the type
This astype pandas method will take dataframe column as input and convert the data type to float.
We can convert to float by specifying a keyword called 'float'. But we have to specify the old datatype of the column to be converted.
Syntax:
dataframe['column'].astype(old_datatype).astype(float)
where,
1. dataframe is the input dataframe
2. column is the name of the column in which the datatype to be converted.
3. old_datatype is the datatype of the column (like str,int etc)
Note - In pandas DataFrame, the string type column will be considered as an object.
Example: convert column to float in pandas example
In this convert column to float pandas example, we will convert Total Staff column to float type
import pandas as pd
#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],
'college_name':["vignan university","vvit","RVR - JC","Andhra University"],
"college_address":["guntur","guntur","guntur","guntur"],
"Total Staff":['1200','3422','5644','670']
},index=['one','two','three','four'])
# convert Total Staff column to float type
data['Total Staff'] = data['Total Staff'].astype(str).astype(float)
# display the datatypes
print(data.dtypes)
Output: convert column to float pandas result for total staff column
college_id object
college_name object
college_address object
Total Staff float64
dtype: object
From the output we observed that data type of Total Staff column is object before and now it is converted to Float type.
In this way we can convert column to float in pandas.
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Mar 15,2022