Pandas Convert Column to String Type?

  • Post author:
  • Post category:Pandas / Python
  • Post last modified:January 28, 2023

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 covert 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

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

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

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

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

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

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

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

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

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

# 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(df)
print(df.dtypes)

Yields below output.


# Output:
   Courses    Fee Duration  Discount
0    Spark  22000    30day      1000
1  PySpark  25000   50days      2300
2   Hadoop  23000   55days      1000
3   Python  24000   40days      1200
4   Pandas  26000   60days      2500
5   Hadoop  25000    35day      1300
6    Spark  25000   55days      1400

Courses     object
Fee          int64
Duration    object
Discount     int64
dtype: object

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(df.dtypes)

Yields below output.


# Output:
Courses     object
Fee         string
Duration    object
Discount     int64
dtype: object

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 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(df.dtypes)

Yields below output.


# Output:
Courses     object
Fee         string
Duration    object
Discount     int64
dtype: object

4. Convert Multiple Columns to String

You can also convert multiple columns to string by sending dict of column name -> data type to astype() method. The below example converts 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(df.dtypes)

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

# Multiple columns string conversion
df["Fee"] = df["Fee"].astype(str)
df["Discount"]= df["Discount"].astype(str)
print(df.dtypes)

Yields below output.


# Output:
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(df.dtypes)

Yields below output.


# Output:
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(df.dtypes)

Yields 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(df.dtypes)

Yields 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(df.dtypes)

# Convert entire DataFrame to string
df=df.astype(str)
print(df.dtypes)

Yields below output.


# Output:
Courses     object
Fee         object
Duration    object
Discount    object
dtype: object

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 DataFrame.map(str) and DataFrame.applymap(str) methods.

Happy Learning !!

References

Leave a Reply

You are currently viewing Pandas Convert Column to String Type?