Open In App

Convert a Dataframe Column to Integer in Pandas

Last Updated : 31 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

When we use data in Python it often involves using Pandas, a powerful library for data manipulation and analysis. When dealing with DataFrames, it’s common to encounter columns with data types that need to be converted for further analysis or visualization. One frequent task is converting a column to an integer type. In this article, we’ll explore some methods to achieve this using Python and Pandas.

Convert a Dataframe Column to Integer in Pandas

Below, are the ways to Convert a Pandas Dataframe Column to Integer in Python Pandas.

Convert a Dataframe Column to Integer Using astype() method

In this example, the below code utilizes the Pandas library to create a DataFrame named ‘df’ with a single column ‘Column1’ containing string values. The astype() method is then employed to convert the ‘Column1’ values to integers. Then DataFrame is printed, and the data type of ‘Column1’ is displayed using the ‘dtype’ attribute.

Python3




import pandas as pd
 
# Creating a sample DataFrame
data = {'Column1': ['1', '2', '3', '4']}
df = pd.DataFrame(data)
 
#Using astype() method
df['Column1'] = df['Column1'].astype(int)
print(df)
df['Column1'].dtype


Output :

Column1
0 1
1 2
2 3
3 4
dtype('int64')

Convert a Dataframe Column to Integer Using to_numeric() method

In this eample, below code uses pandas to create a DataFrame with a string column (‘Column1’). The ‘to_numeric()’ method converts the column to integers, optimizing memory usage. Conversion errors are handled by replacing them with NaN. The resulting DataFrame is printed, and the data type of ‘Column1’ is displayed.

Python3




import pandas as pd
 
# Creating a sample DataFrame
data = {'Column1': ['1', '2', '3', '4']}
df = pd.DataFrame(data)
 
# Using to_numeric() method
df = pd.DataFrame(data)
df['Column1'] = pd.to_numeric(df['Column1'], downcast='integer', errors='coerce')
print(df)
df['Column1'].dtype


Output :

  Column1
0 1
1 2
2 3
3 4
dtype('int8')

Convert a Dataframe Column to Integer Using apply() method

In this example, below code creates a pandas DataFrame ‘df’ with a string column ‘Column1’. The ‘apply()’ method, combined with a lambda function, is used to convert each value in ‘Column1’ to integers. The DataFrame is printed, and the data type of ‘Column1’ is displayed using the ‘dtype’ attribute.

Python3




import pandas as pd
 
# Creating a sample DataFrame
data = {'Column1': ['1', '2', '3', '4']}
df = pd.DataFrame(data)
 
# Using apply() method with a lambda function
df = pd.DataFrame(data)
df['Column1'] = df['Column1'].apply(lambda x: int(x))
print(df)
df['Column1'].dtype


Output :

 Column1
0 1
1 2
2 3
3 4
dtype('int64')

Conclusion

In conclusion, converting a DataFrame column to an integer in Python is a straightforward process that involves using the astype() method or the pd.to_numeric() function. Careful consideration of data types, handling missing values, and ensuring compatibility with the desired integer format are essential steps in this conversion. Additionally, exploring alternative methods such as custom functions or lambda expressions can provide flexibility based on specific requirements



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads