Convert Columns to String in Pandas
Create a sample dataset:
Python
import pandas as pd import numpy as np # Creating a DataFrame with random numerical and string columns np.random.seed( 42 ) # Setting seed for reproducibility data = { 'Numeric_Column' : np.random.randint( 1 , 100 , 4 ), 'String_Column' : np.random.choice([ 'A' , 'B' , 'C' , 'D' ], 4 ) } df = pd.DataFrame(data) df.info() |
Output:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Numeric_Column 4 non-null int64
1 String_Column 4 non-null object
dtypes: int64(1), object(1)
Method 1: Using astype() Method:
The astype() method in Pandas is used to change the data type of a column. It’s a simple and efficient way to convert the data type of a specific column to another. In this case, we use it to convert a numeric column to a string.
Python
# Using astype() Method df_astype = df.copy() df_astype[ 'Numeric_Column' ] = df_astype[ 'Numeric_Column' ].astype( str ) print ( "\nDataFrame after converting 'Numeric_Column' to string using astype():" ) print (df_astype) print (df_astype.info()) |
Output:
DataFrame after converting 'Numeric_Column' to string using astype():
Numeric_Column String_Column
0 52 A
1 93 A
2 15 C
3 72 B
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Numeric_Column 4 non-null object
1 String_Column 4 non-null object
dtypes: object(2)
memory usage: 192.0+ bytes
Method 2:Using the map() Function:
The map() function in Pandas is a versatile tool for element-wise transformations. It is commonly used to apply a function to each element of a Series. Here, we use map() to convert the ‘Numeric_Column’ to strings.
Python
df_map = df.copy() df_map[ 'Numeric_Column' ] = df_map[ 'Numeric_Column' ]. map ( str ) print ( "\nDataFrame after converting 'Numeric_Column' to string using map():" ) print (df_map) print (df_map.info()) |
Output:
DataFrame after converting 'Numeric_Column' to string using map():
Numeric_Column String_Column
0 52 A
1 93 A
2 15 C
3 72 B
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Numeric_Column 4 non-null object
1 String_Column 4 non-null object
dtypes: object(2)
memory usage: 192.0+ bytes
Method 3: Using the apply() Function:
The apply() function in Pandas is a powerful tool that allows you to apply a custom function along the axis of a DataFrame. Here, we use apply() to apply the str function to each element of the ‘Numeric_Column’, converting numeric values to strings.
Python
# Utilizing apply() Function df_apply = df.copy() df_apply[ 'Numeric_Column' ] = df_apply[ 'Numeric_Column' ]. apply ( str ) print ( "\nDataFrame after converting 'Numeric_Column' to string using apply():" ) print (df_apply) print (df_apply.info()) |
Output:
DataFrame after converting 'Numeric_Column' to string using apply():
Numeric_Column String_Column
0 52 A
1 93 A
2 15 C
3 72 B
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Numeric_Column 4 non-null object
1 String_Column 4 non-null object
dtypes: object(2)
memory usage: 192.0+ bytes
How to Convert Pandas Columns to String
Pandas, a powerful data manipulation library for Python, provides extensive functionality for handling and transforming data. One common task is converting columns to strings, which is useful in scenarios where you need to perform string operations on numerical or categorical data. In this blog post, we’ll explore the concepts related to converting columns to strings in Pandas and delve into three different methods to achieve this.