7 ways to convert pandas DataFrame column to float


Python Pandas

Different methods to convert column to float in pandas DataFrame

In this tutorial we will discuss how to convert DataFrame columns into float using the following methods:

Convert integer type column to float:

  • Using astype() method
  • Using astype() method with dictionary
  • Using astype() method by specifying data types

Convert string/object type column to float:

  • Using astype() method
  • Using astype() method with dictionary
  • Using astype() method by specifying data types

Convert to float using convert_dtypes()

 

Create pandas DataFrame with example data

DataFrame is a data structure used to store the data in two dimensional format. It is similar to table that stores the data in rows and columns. Rows represents the records/ tuples and columns refers to the attributes.

We can create the DataFrame by using pandas.DataFrame() method.

Syntax:

pandas.DataFrame(input_data,columns,index)

Parameters:

It will take mainly three parameters

  1. input_data is represents a list of data
  2. columns represent the columns names for the data
  3. index represent the row numbers/values

We can also create a DataFrame using dictionary by skipping columns and indices.

 

Example: Python Program to create a dataframe for market data from a dictionary of food items by specifying the column names.

#import the module
import pandas

#consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567,562,67,76],
                  'quantity':['1','2','3','2']}

#pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

#display the dataframe data types
print(dataframe.dtypes)

Output:

id          object
name        object
cost         int64
quantity    object
dtype: object

 

Method 1 : Convert integer type column to float using astype() method

Here we are going to convert the integer type column in DataFrame to integer type using astype() method. we just need to pass float keyword inside this method.

Syntax:

 dataframe['column'].astype(float)

where,

  1. dataframe is the input dataframe
  2. column is the integer type column to be converted to float

Example:

Python program to convert cost column to float

#import the module
import pandas

#consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567,562,67,76],
                  'quantity':['1','2','3','2']}

#pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

#convert the cost column data type (integer) into float
dataframe['cost'] = dataframe['cost'].astype(float)

#display data types
print(dataframe.dtypes)

Output:

id           object
name         object
cost        float64
quantity     object
dtype: object

 

Method 2 : Convert integer type column to float using astype() method with dictionary

Here we are going to convert the integer type column in DataFrame to float type using astype() method. we just need to pass float keyword inside this method through dictionary.

Syntax:

 dataframe['column'].astype({"column":float})

where,

  1. dataframe is the input dataframe
  2. column is the integer type column to be converted to float

 

Example: Python program to convert cost column to float

#import the module
import pandas

#consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567.00,562.56,67.00,76.09],
                  'quantity':['1','2','3','2']}

#pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

#convert the cost column data type (integer) into float
dataframe = dataframe.astype({"cost": float})

#display data types
print(dataframe.dtypes)

Output:

id           object
name         object
cost        float64
quantity     object
dtype: object

 

Method 3 : Convert integer type column to float using astype() method by specifying data types

Here we are going to use astype() method twice by specifying types. first method takes the old data type i.e int and second method take new data type i.e float type

Syntax:

dataframe['column'].astype(int).astype(float)

 

Example: Python program to convert cost column to float

#import the module
import pandas

#consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567,562,67,76],
                  'quantity':['1','2','3','2']}

#pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

#convert the cost column data type (integer) into float
dataframe['cost'] = dataframe['cost'].astype(int).astype(float)

#display data types
print(dataframe.dtypes)

Output:

id           object
name         object
cost        float64
quantity     object
dtype: object

 

Method 4 : Convert string/object type column to float using astype() method

Here we are going to convert the string type column in DataFrame to float type using astype() method. we just need to pass float keyword inside this method.

Syntax:

 dataframe['column'].astype(float)

where,

  1. dataframe is the input dataframe
  2. column is the string type column to be converted to float

 

Example: Python program to convert quantity column to float

#import the module
import pandas

#consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567,562,67,76],
                  'quantity':['1','2','3','2']}

#pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

#convert the quantity column data type (string) into float
dataframe['quantity'] = dataframe['quantity'].astype(float)

#display data types
print(dataframe.dtypes)

Output:

id           object
name         object
cost          int64
quantity    float64
dtype: object

 

Method 5 : Convert string/object type column to float using astype() method with dictionary

Here we are going to convert the string type column in DataFrame to float type using astype() method. we just need to pass float keyword inside this method through dictionary.

Syntax:

 dataframe['column'].astype({"column":float})

where,

  1. dataframe is the input dataframe
  2. column is the string type column to be converted to float

 

Example: Python program to convert quantity column to float

#import the module
import pandas

#consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567,562,67,76],
                  'quantity':['1','2','3','2']}

#pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

#convert the quantity column data type (string) into integer
dataframe = dataframe.astype({"quantity": float})

#display data types
print(dataframe.dtypes)

Output:

id           object
name         object
cost          int64
quantity    float64
dtype: object

 

Method 6 : Convert string/object type column to float using astype() method by specifying data types

Here we are going to use astype() method twice by specifying types. first method takes the old data type i.e string and second method take new data type i.e float type

Syntax:

dataframe['column'].astype(str).astype(float)

 

Example: Python program to convert quantity column to float

#import the module
import pandas

#consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567,562,67,76],
                  'quantity':['1','2','3','2']}

#pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

#convert the quantity column data type (string) into float
dataframe['quantity'] = dataframe['quantity'].astype(str).astype(float)

#display data types
print(dataframe.dtypes)

Output:

id           object
name         object
cost          int64
quantity    float64
dtype: object

 

Method 7 : Convert to float using convert_dtypes()

Here we are going to use convert_dtypes() method. It will automatically convert into float type.

Syntax:

dataframe.convert_dtypes()

Example: Python program to convert dataframe columns to float

#import the module
import pandas

#consider the food data
food_input={'id':['foo-23','foo-13','foo-02','foo-31'],
                  'name':['ground-nut oil','almonds','flour','cereals'],
                  'cost':[567.00,562.56,67.00,76.09],
                  'quantity':['1','2','3','2']}

#pass this food to the dataframe by specifying rows 
dataframe=pandas.DataFrame(food_input,index = ['item-1', 'item-2', 'item-3', 'item-4'])

#convert into int type
dataframe = dataframe.convert_dtypes()

print(dataframe.dtypes)

Output:

id           string
name         string
cost          int64
quantity     string
dtype: object
dtype: object

 

Summary

In this tutorial we discussed how to convert dataframe column to float type using astype() method through 7 scenarios by considering integer and string/object (str) types. Here Dictionary is involved in two methods to convert the data type.

 

Reference

Pandas astype()

 

Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment