Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

Converting Columns to String in Pandas DataFrame

  • 07 Jan, 2025
  • Com 0
Featured Image of Converting Single or Multiple Columns of DataFrame to String

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

Using astype() function to convert a column to string

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

Output of using astype()

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

Convert 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

Output of Converting all columns to string

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

Output of using .apply() and .map() functions

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

Post Views: 65
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Converting Floats to Integers in a Pandas DataFrame
Printing DataFrame without an Index in Pandas

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend