Convert the data type of Pandas column to int
Last Updated :
13 Jan, 2021
In this article, we are going to see how to convert a Pandas column to int. Once a pandas.DataFrame is created using external data, systematically numeric columns are taken to as data type objects instead of int or float, creating numeric tasks not possible. We will pass any Python, Numpy, or Pandas datatype to vary all columns of a dataframe thereto type, or we will pass a dictionary having column names as keys and datatype as values to vary the type of picked columns.
Here astype() function empowers us to be express the data type you need to have. It's extremely adaptable i.e you can attempt to go from one type to some other.
Approach:
- Import pandas
- Initialize DataFrame
- Apply function to DataFrame column
- Print data type of column
Example 1:
We first imported pandas module using the standard syntax. Then we created a dataframe with values 1, 2, 3, 4 and column indices as a and b. We named this dataframe as df. Next we converted the column type using the astype() method. The final output is converted data types of column.
Code:
Python
import pandas as pd
df = pd.DataFrame([["1", "2"], ["3", "4"]],
columns = ["a", "b"])
df["a"] = df["a"].astype(str).astype(int)
print(df.dtypes)
Output:
Example 2:
We first imported the pandas module using the standard syntax. Then we created a dataframe with values 'A': [1, 2, 3, 4, 5], 'B': ['a', 'b', 'c', 'd', 'e'], 'C': [1.1, '1.0', '1.3', 2, 5] and column indices as A, B and C. We used dictionary named convert_dict to convert specific columns A and C. We named this dataframe as df. Next, we converted the column type using the astype() method. The final output is converted data types of columns.
Python
import pandas as pd
# sample dataframe
df = pd.DataFrame({'A': [1, 2, 3, 4, 5],
'B': ['a', 'b', 'c', 'd', 'e'],
'C': [1.1, '1.0', '1.3', 2, 5] })
# using dictionary to convert specific columns
convert_dict = {'A': int,
'C': float }
df = df.astype(convert_dict)
print(df.dtypes)
Output:
Similar Reads
Pandas Convert Column To String Type Pandas is a Python library widely used for data analysis and manipulation of huge datasets. One of the major applications of the Pandas library is the ability to handle and transform data. Mostly during data preprocessing, we are required to convert a column into a specific data type. In this articl
4 min read
Get the data type of column in Pandas - Python Letâs see how to get data types of columns in the pandas dataframe. First, Letâs create a pandas dataframe. Example: Python3 # importing pandas library import pandas as pd # List of Tuples employees = [ ('Stuti', 28, 'Varanasi', 20000), ('Saumya', 32, 'Delhi', 25000), ('Aaditya', 25, 'Mumbai', 40000
3 min read
How to Convert Index to Column in Pandas Dataframe? Pandas is a powerful tool which is used for data analysis and is built on top of the python library. The Pandas library enables users to create and manipulate dataframes (Tables of data) and time series effectively and efficiently. These dataframes can be used for training and testing machine learni
2 min read
Convert Pandas Dataframe Column To Float Converting columns to floats in Pandas DataFrame is a very crucial step for data analysis. Converting columns to float values can help you perform various arithmetic operations and plot graphs.In this article, weâll look at different ways to convert a column to a float in DataFrame.Using DataFrame.a
6 min read
Convert a Dataframe Column to Integer in Pandas Converting DataFrame columns to the correct data type is important especially when numeric values are mistakenly stored as strings. Let's learn how to efficiently convert a column to an integer in a Pandas DataFrameConvert DataFrame Column to Integer - using astype() Methodastype() method is simple
3 min read
How to Convert Pandas Columns to String Converting columns to strings allows easier manipulation when performing string operations such as pattern matching, formatting or concatenation. Pandas provides multiple ways to achieve this conversion and choosing the best method can depend on factors like the size of your dataset and the specific
3 min read