7 ways to convert pandas DataFrame column to int


Python Pandas

Different methods to convert column to int in pandas DataFrame

In this tutorial we will discuss how to convert DataFrame columns into int 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 int

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

Convert to int 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.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'])

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

Output:

id           object
name         object
cost        float64
quantity     object
dtype: object

 

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

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

Syntax:

 dataframe['column'].astype(int)

where,

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

 

Example: Python program to convert cost column to int

# 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 (float) into integer
dataframe['cost'] = dataframe['cost'].astype(int)

# display data types
print(dataframe.dtypes)

Output:

id          object
name        object
cost         int64
quantity    object
dtype: object

 

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

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

Syntax:

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

where,

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

 

Example: Python program to convert cost column to int

# 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 (float) into integer
dataframe = dataframe.astype({"cost": int})

# display data types
print(dataframe.dtypes)

Output:

id          object
name        object
cost         int64
quantity    object
dtype: object

 

Method 3 : Convert float type column to int 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 float and second method take new data type i.e integer type

Syntax:

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

 

Example: Python program to convert cost column to int

# 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 (float) into integer
dataframe['cost'] = dataframe['cost'].astype(float).astype(int)

# display data types
print(dataframe.dtypes)

Output:

id          object
name        object
cost         int64
quantity    object
dtype: object

 

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

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

Syntax:

 dataframe['column'].astype(int)

where,

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

 

Example: Python program to convert quantity column to int

# 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 quantity column data type (string) into integer
dataframe['quantity'] = dataframe['quantity'].astype(int)

# display data types
print(dataframe.dtypes)

Output:

id           object
name         object
cost        float64
quantity      int64
dtype: object

 

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

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

Syntax:

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

where,

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

 

Example: Python program to convert quantity column to int

# 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 quantity column data type (string) into integer
dataframe = dataframe.astype({"quantity": int})

# display data types
print(dataframe.dtypes)

Output:

id           object
name         object
cost        float64
quantity      int64
dtype: object

 

Method 6 : Convert string/object type column to int 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 integer type

Syntax:

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

 

Example: Python program to convert quantity column to int

# 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 quantity column data type (string) into integer
dataframe['quantity'] = dataframe['quantity'].astype(str).astype(int)

# display data types
print(dataframe.dtypes)

Output:

id           object
name         object
cost        float64
quantity      int64
dtype: object

 

Method 7 : Convert to int using convert_dtypes()

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

Syntax:

dataframe.convert_dtypes()

Example: Python program to convert dataframe columns to int

# 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        float64
quantity     string
dtype: object
dtype: object

 

Summary

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

 

References

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