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
input_data
is represents a list of datacolumns
represent the columns names for the dataindex
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,
- dataframe is the input dataframe
- 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,
- dataframe is the input dataframe
- 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,
- dataframe is the input dataframe
- 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,
- dataframe is the input dataframe
- 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