10.7 C
London
Sunday, July 7, 2024
HomePandas in PythonDataFrame Functions in PythonHow to Convert Pandas DataFrame Columns to Strings

How to Convert Pandas DataFrame Columns to Strings

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

Often you may wish to convert one or more columns in a pandas DataFrame to strings. Fortunately this is easy to do using the built-in pandas astype(str) function.

This tutorial shows several examples of how to use this function.

Example 1: Convert a Single DataFrame Column to String

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'player': ['A', 'B', 'C', 'D', 'E'],
                   'points': [25, 20, 14, 16, 27],
                   'assists': [5, 7, 7, 8, 11]})

#view DataFrame 
df

        player	points	assists
0	A	25	5
1	B	20	7
2	C	14	7
3	D	16	8
4	E	27	11

We can identify the data type of each column by using dtypes:

df.dtypes

player     object
points      int64
assists     int64
dtype: object

We can see that the column “player” is a string while the other two columns “points” and “assists” are integers.

We can convert the column “points” to a string by simply using astype(str) as follows:

df['points'] = df['points'].astype(str)

We can verify that this column is now a string by once again using dtypes:

df.dtypes

player     object
points     object
assists     int64
dtype: object

Example 2: Convert Multiple DataFrame Columns to Strings

We can convert both columns “points” and “assists” to strings by using the following syntax:

df[['points', 'assists']] = df[['points', 'assists']].astype(str)

And once again we can verify that they’re strings by using dtypes:

df.dtypes

player     object
points     object
assists    object
dtype: object

Example 3: Convert an Entire DataFrame to Strings

Lastly, we can convert every column in a DataFrame to strings by using the following syntax:

#convert every column to strings
df = df.astype(str)

#check data type of each column
df.dtypes
player     object
points     object
assists    object
dtype: object

You can find the complete documentation for the astype() function here.

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories