Python dataframe column to int

7 ways to convert pandas DataFrame column to int

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.

Читайте также:  Or die header php

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

pandas.DataFrame(input_data,columns,index)

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)
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.

  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)
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 .

  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() # display data types print(dataframe.dtypes) 
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

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) 
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.

  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)
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.

  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() # display data types print(dataframe.dtypes) 
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

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) 
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.

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)
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

Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud

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 either use the comments section or contact me form.

Thank You for your support!!

Источник

How to convert Pandas DataFrame columns to int types?

EasyTweaks.com

In order to convert one or more pandas DataFrame columns to the integer data type use the astype() method. Here’s a simple example:

# single column / series my_df['my_col'].astype('int64') # for multiple columns my_df.astype()

In this tutorial, we will look into three main use cases:

  • Casting a specific column from float to int
  • Convert a column containing nan empty values to int
  • Converting multiple columns to int / int64

Sample Pandas DataFrame

Let’s get started by writing some simple Python code that will help us to create some test data that you can use to follow along.

 import pandas pd #Lists containing test data offices = ['Paris', 'Madrid', 'London', 'Barcelona', 'Brussels'] num_interviews = [129.0, 132.0, 145.0, 230.0, pd.NA] positions = [12.0, 15.0, 13.0, 13.5, 3] #Create pandas DataFrame from dictionary interviews_dict = dict(office=offices, total_interviews =num_interviews, total_positions = positions ) interviews = pd.DataFrame(interviews_dict) interviews.head() print(interviews.head()) 
office total_interviews total_positions
0 Paris 129.0 12.0
1 Madrid 132.0 15.0
2 London 145.0 13.0
3 Barcelona 230.0 13.5
4 Brussels 3.0

Let’s find out the data types for the different DataFrame columns:

office object total_interviews object total_positions float64 dtype: object

Convert a single column from float to integer

We will start by converting a single column from float64 to int and int64 data types.

interviews['total_positions'].astype('int')

This will return a series casted to int. To change the type to int64, simply type:

 interviews['total_positions'].astype('int64') 

Handling conversion of columns to int with nan values

You might have noted that one of our DataFrame columns contains an empty value. Trying to cast it to integer will render the following error:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'NAType'

We should therefore handle the empty values first and then go ahead and cast the column:

interviews['total_interviews'].fillna(0, inplace=True).astype(int)

Converting multiple columns to int types

Let us look into a more realistic scenario in which we cast multiple columns at the same time. We’ll first go ahead and take care of cells containing empty values.

 interviews.fillna(0, inplace=True)

We’ll then cast multiple columns to int64. Unlike before, we’ll pass a dictionary containing the columns to convert and the required dtype for each.

interviews_2 = interviews.astype()

We’ll finish up by verifying the data types:

office object total_interviews int64 total_positions int64 dtype: object

Rename converted columns

Last, we can go ahead and rename the columns that you just converted. Also here. we’ll pass a mapping dictionary as a parameter to the DataFrame method. Here’s a short snippet:

interviews.rename(mapper = , axis=1, inplace=True)

Recent Posts

Источник

Оцените статью