• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:15 mins read
You are currently viewing Pandas Convert Column to String Type

In this article, I will explain how to convert single column or multiple columns to string type in pandas DataFrame, here, I will demonstrate using DataFrame.astype(str), DataFrame.values.astype(str), DataFrame.apply(str), DataFrame.map(str), and DataFrame.applymap(str) methods to convert any type to string type.

1. Quick Examples of Convert Column To String

If you are in a hurry, below are some of the quick examples of how to convert column to string type in Pandas DataFrame. You can apply these to convert from/to any type in Pandas.

Note that map(str) and apply(str) takes less time compared with the remaining techniques.


# Below are the quick examples

# Example 1: Convert "Fee" from int to string
df = df.astype({'Fee':'string'})

# Example 2: Using Series.astype() to convert to string 
df["Fee"]=df["Fee"].values.astype('string')

# Example 3: Multiple columns string conversion
df = pd.DataFrame(technologies)
df = df.astype({'Fee':'string','Discount':'string'})

# Example 4: Multiple columns string conversion
df = pd.DataFrame(technologies)
df[[ 'Fee', 'Discount']] = df[['Fee','Discount']].astype(str)

# Example 5: Multiple columns string conversion
df["Fee"] = df["Fee"].astype(str)
df["Discount"]= df["Discount"].astype(str)

# Example 6: Using apply(str) method
df["Fee"]=df["Fee"].apply(str)

# Example 7: Using apply(str) with lambda function
df["Fee"] = df["Fee"].apply(lambda x: str(x))

# Example 8: Using map(str) method
df['Fee'] = df["Fee"].map(str)

# Example 9: Convert entire DataFrame to string
df=df.applymap(str)

# Example 10: Convert entire DataFrame to string
df=df.astype(str)

Now, let’s see a detailed example. first, create a Pandas DataFrame with a few rows and columns, and execute and validate the results. Our DataFrame contains column names Courses, Fee, Duration, and Discount.


import pandas as pd
import numpy as np
technologies= ({
   'Courses':["Spark","PySpark","Hadoop","Python","Pandas","Hadoop","Spark"],
    'Fee' :[22000,25000,23000,24000,26000,25000,25000],
    'Duration':['30day','50days','55days','40days','60days','35day','55days'],
    'Discount':[1000,2300,1000,1200,2500,1300,1400]
              })
df = pd.DataFrame(technologies)
print("Create DataFrame:\n", df)
print("-------------------------------------------------")
print("Get type of the columns of DataFrame:\n", df.dtypes)

Yields below output.

Pandas Convert Columns String

You can identify the data type of each column by using dtypes.

2. Convert Column to String Type

Use pandas DataFrame.astype() function to convert a column from int to string, you can apply this on a specific column or on an entire DataFrame.

The Below example converts Fee column from int to string dtype. You can also use numpy.str_ or 'str'to specify string type.


# Convert "Fee" from int to string
df = df.astype({'Fee':'string'})
print("After converting a specific column to string:\n", df.dtypes)

Yields below output.

Pandas Convert Columns String

3. Convert Specific Column to String

You can also use Series.astype() to convert a specific column. Since each column on DataFrame is Pandas Series, I will get the column from DataFrame as a Series and use astype() function. In the below example df.Fee or df['Fee'] returns Series object.


# Using Series.astype() to convert column to string 
df["Fee"]=df["Fee"].values.astype('string')
print("After converting a specific column to string:\n", df.dtypes)

Yields below output.


# Output:
# After converting a specific column to string:
Courses     object
Fee         string
Duration    object
Discount     int64
dtype: object

4. Convert Multiple Columns to String

You can also convert multiple columns to strings by sending a dict of column names to astype() method. The below example converts the column Fee from int to string and Discount from float to string dtype.


# Multiple columns string conversion
df = pd.DataFrame(technologies)
df = df.astype({'Fee':'string','Discount':'string'})
print("After converting multiple columns to string:\n", df.dtypes)

# Multiple columns string conversion
df = pd.DataFrame(technologies)
df[[ 'Fee', 'Discount']] = df[['Fee','Discount']].astype(str)
print("After converting multiple columns to string:\n", df.dtypes)

# Multiple columns string conversion
df["Fee"] = df["Fee"].astype(str)
df["Discount"]= df["Discount"].astype(str)
print("After converting multiple columns to string:\n", df.dtypes)

Yields below output.


# Output:
# After converting multiple columns to string:
Courses     object
Fee         object
Duration    object
Discount    object
dtype: object

5. Using DataFrame.apply(str)

You can convert the column “Fee” to a string by simply using DataFrame.apply(str) , for example df["Fee"]=df["Fee"].apply(str) .


# Using apply(str) method
df["Fee"]=df["Fee"].apply(str)
print("After converting a specific column to string:\n", df.dtypes)

Yields below output.


# Output:
# After converting a specific column to string:
Courses     object
Fee         object
Duration    object
Discount     int64
dtype: object

Using apply() with a lambda expression also works in this case. For example df["Fee"] = df["Fee"].apply(lambda x: str(x)).


# Using apply(str) to lambda function
df["Fee"] = df["Fee"].apply(lambda x: str(x))
print("After converting a specific column to string:\n", df.dtypes)

Yields the same output as above.

6. Using Series.map(str)

You can also convert the column “Fee” to a string by using Series.map(str). for example df['Fee']=df["Fee"].map(str).


# Convert columns to string using map(str) method
df['Fee'] = df["Fee"].map(str)
print("After converting a specific column to string:\n", df.dtypes)

Yields the same output as above.

Note: map(str) and apply(str) takes less time to compare with the remaining techniques.

7. Convert All Columns to Strings

If you want to change the data type for all columns in the DataFrame to the string type, you can use df.applymap(str) or df.astype(str) methods.


# Convert entire DataFrame to string
df=df.applymap(str)
print("After converting all columns to string:\n", df.dtypes)

# Convert entire DataFrame to string
df=df.astype(str)
print("After converting all columns to string:\n", df.dtypes)

Yields below output.


# Output:
# After converting all columns to string:
Courses     object
Fee         object
Duration    object
Discount    object
dtype: object

Frequently Asked Questions on

How do I convert a column to a string type in Pandas?

You can convert a column to a string type in Pandas using the astype() method. For example, if you have a DataFrame df and you want to convert a column named ‘Fee’ to a string type, you can use this code df['Fee'] = df['Fee'].astype(str)

What if I want to convert multiple columns to string type?

If you want to convert multiple columns to string type, you can pass a list of column names to the astype() method. For example, df[['Fee', 'Discount']] = df[['Fee', 'Discount']].astype(str)

How can I convert a specific column to a string while keeping other columns as they are?

You can convert a specific column to a string type while keeping other columns as they are. For example, to convert 'Fee' to a string and leave the rest of the DataFrame unchanged. For example, df['Fee'] = df['Fee'].astype(str)

What if the column contains NaN (missing) values?

If the column contains NaN values, you can still convert it to a string. The NaN values will be converted to the string ‘nan’

Conclusion

In this article, you have learned how to convert columns to string type in pandas using DataFrame.astype(str), Series.astype(str) and DataFrame.apply(str) methods. Also, you have learned how to convert to string using df.map(str) and df.applymap(str) methods.

Happy Learning !!

References

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium

Leave a Reply