You can convert any columns of a DataFrame to string in Pandas using the “astype()” or “apply()” function.
In real life, when you are working on a DataFrame, you might have mixed data type columns, and that’s where you need a conversion that ensures consistency for operations like concatenation, comparison, or writing to certain file formats.
Method 1: Using astype()
The .astype() method in Pandas explicitly changes the data type of a Series or column of a DataFrame (or an entire DataFrame).
If you want to convert it into a String, pass “str” to the astype() method. This is the fastest approach.
Basic conversion
import pandas as pd # Data to be used data = {'col1': [1.23, 4.56, 21.19], 'col2': [11, 19, 21]} # Create a DataFrame df = pd.DataFrame(data) print("Before conversion: ") print(df.dtypes) # Convert the datatype of the column 'col1' # Using .astype() method df['col1'] = df['col1'].astype(str) print("After conversion: ") print(df.dtypes)
Output
You can see from the above output image that the data type is changed from “float64” to “object”. You can represent “string” as an “object” in Pandas.
To verify the conversion, we used the df.dtypes property.
Converting all columns of DataFrame to String
If you want to convert all columns of a DataFrame, you can do this using this syntax: df.astype(str)
import pandas as pd # Data to be used data = {'col1': [1.23, 4.56, 21.19], 'col2': [11, 19, 21]} # Create a DataFrame df = pd.DataFrame(data) print("Before conversion: ") print(df.dtypes) # Convert entire DataFrame to string # Using .astype() method df = df.astype(str) print("After conversion: ") print(df.dtypes)
Output
Dealing with missing values (NaN)
By default, the astype(str) method will convert NaN values to the string “nan”.
You can handle NaN values differently. For example, you can keep them as NaN or replace them with an empty string using the fillna() method before conversion.
df['col1'] = df['col1'].fillna('').astype(str) # Replace NaN with empty string
Method 2: Using apply() and map()
Pandas library provides an .apply() function that can be used to convert specific columns.
If you want to convert all columns to string (object) type, you can use the “map()” method.
import pandas as pd # Data to be used data = {'col1': [1.23, 4.56, 21.19], 'col2': [11, 19, 21]} # Create a DataFrame df = pd.DataFrame(data) print("Before conversion: ") print(df.dtypes) # Print dtypes of the entire DataFrame # Convert ONLY col1 to string using .apply() df['col1'] = df['col1'].apply(str) print("\nAfter converting col1 to string using .apply(): ") print(df.dtypes) # Print dtypes of the entire DataFrame again # To convert the ENTIRE DataFrame to string using .apply(): df_all_str = df.map(str) # For applying to every element in the DataFrame print("\nAfter converting ENTIRE DataFrame to string using .map():") print(df_all_str.dtypes) print("\nDataFrame after converting ENTIRE DataFrame to string using .map():") print(df_all_str)
Output
The apply() method is really helpful when you want to do complex transformations that require custom logic or when operating on rows/columns of a DataFrame.
If you want to do element-wise transformations on a Series (column of a DataFrame), especially for mapping values from one set to another (using a dictionary).