. Advertisement .
..3..
. Advertisement .
..4..
Looking for ways to convert column to string type in Pandas? You are at the right place! This guide will show you how to do so by utilizing DataFrame.values.astype(str), DataFrame.astype(str), DataFrame.apply(str), DataFrame.applymap(str), and DataFrame.map(str).
We also provide many examples for each method so you can use them for reference. Ready to learn? Let’s get started!
Convert Column To String In Pandas Examples
Here are a few brief examples of converting a column in a Pandas DataFrame to a string type. You can use these methods for converting from or to all types.
Notice that apply(str)
and map(str)
run faster than the other methods.
# Below are quick example
# 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)
Here is a complete example. Build a DataFrame in Pandas with columns and rows, run it, and verify the outcomes. In this DataFrame, there are Course, Duration, Fee, and Discount columns.
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)
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
Utilizing dtypes, you may determine each column’s data type.
Convert Column In DataFrame To String Type
To change an int column to a string, use the Pandas’s DataFrame.astype() function. This can be applied to a single column or the full DataFrame.
The example below changes the column Fee from an int to a string dtype. To indicate a string type, you can alternatively utilize ‘str’ or numpy.str_
.
# Convert "Fee" from int to string
df = df.astype({'Fee':'string'})
print(df.dtypes)
Output:
Courses object
Fee string
Duration object
Discount int64
dtype: object
Convert Single Column To String
You may also convert a particular column using Series.astype()
. As every column in a DataFrame is a pandas Series, you can use the astype() function to obtain the column in the DataFrame as a Series. In the example below, df[‘Fee’]
or df.Fee
gives back a Series Python object.
# Using Series.astype() to convert column to string
df["Fee"]=df["Fee"].values.astype('string')
print(df.dtypes)
Output:
Courses object
Fee string
Duration object
Discount int64
dtype: object
Convert Many Columns To String
Additionally, you can convert many columns to strings by giving a dict containing column name -> dtype to the astype()
method. In the following example, we convert the Fee column from integer to string type and the Discount column float value to string.
# 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)
Output:
Courses object
Fee object
Duration object
Discount object
dtype: object
Utilizing DataFrame.apply(str)
By utilizing DataFrame.apply(str)
, you will easily convert the Fee column to string type. Here is an example of how to do it: df[“Fee”]=df[“Fee”].apply(str)
.
# Using apply(str) method
df["Fee"]=df["Fee"].apply(str)
print(df.dtypes)
Output:
Courses object
Fee object
Duration object
Discount int64
dtype: object
In this case, utilizing apply()
with the lambda expression can also function.
For instance, df[“Fee”] = df[“Fee”].apply(lambda x: str(x)).
Output:
# Using apply(str) to lambda function
df["Fee"] = df["Fee"].apply(lambda x: str(x))
print(df.dtypes)
Utilizing Series.map(str)
Additionally, you may use Series.map(str)
to convert the Fee column into a string. Here is a sample: df[‘Fee’]=df[“Fee”].map(str).
Code:
# Convert columns to string using map(str) method
df['Fee'] = df["Fee"].map(str)
print(df.dtypes)
Notice that apply(str)
and map(str)
consume less time compared to the remaining strategies.
Convert Every Column In DataFrame To Strings
Use the df.astype(str)
or df.applymap(str)
methods to convert all of the columns to the string data type in a DataFrame.
# Convert entire DataFrame to string
df=df.applymap(str)
print(df.dtypes)
# Convert entire DataFrame to string
df=df.astype(str)
print(df.dtypes)
Output:
Courses object
Fee object
Duration object
Discount object
dtype: object
The Bottom Line
Above is our thorough tutorial on how you can convert column to string type in Pandas. Several examples are also included to show you how things are accomplished.After reading this guide, let’s put these methods into practice. You will soon master and easily apply them to your upcoming project. Suppose you want to gain more new skills; why not try learning how to get the names of columns from DataFrame? It will surely be beneficial for your coding journey.
Leave a comment