8.3 C
London
Sunday, March 9, 2025
HomePandas in PythonDataFrame Functions in PythonPandas: How to Replace Empty Strings with NaN

Pandas: How to Replace Empty Strings with NaN

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

You can use the following syntax to replace empty strings with NaN values in pandas:

df = df.replace(r'^s*$', np.nan, regex=True)

The following example shows how to use this syntax in practice.

Related: How to Replace NaN Values with String in Pandas

Example: Replace Empty Strings with NaN

Suppose we have the following pandas DataFrame that contains information about various basketball players:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'B', ' ', 'D', 'E', ' ', 'G', 'H'],
                   'position': [' ', 'G', 'G', 'F', 'F', ' ', 'C', 'C'],
                   'points': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
df

	team	position points	rebounds
0	A		 5	11
1	B	G	 7	8
2		G	 7	10
3	D	F	 9	6
4	E	F	 12	6
5			 9	5
6	G	C	 9	9
7	H	C	 4	12

Notice that there are several empty strings in both the team and position columns.

We can use the following syntax to replace these empty strings with NaN values:

import numpy as np

#replace empty values with NaN
df = df.replace(r'^s*$', np.nan, regex=True)

#view updated DataFrame
df

	team	position points	rebounds
0	A	NaN	 5	11
1	B	G	 7	8
2	NaN	G	 7	10
3	D	F	 9	6
4	E	F	 12	6
5	NaN	NaN	 9	5
6	G	C	 9	9
7	H	C	 4	127

Notice that each of the empty strings have been replaced with NaN.

Note: You can find the complete documentation for the replace function in pandas here.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

How to Impute Missing Values in Pandas
How to Count Missing Values in Pandas
How to Fill NaN Values with Mean in Pandas

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