Table of Contents
Pandas, a popular data analysis library in Python, provides a simple and efficient way to replace empty strings with NaN (Not a Number) values. This can be achieved by using the `replace()` function with the parameter `”` (empty string) and specifying `NaN` as the replacement value. This method allows for easy handling of missing or incomplete data in a dataset, which is a common scenario in data analysis. By replacing empty strings with NaN, Pandas allows for easier data manipulation and calculation without the need for complex conditional statements. This feature makes Pandas a useful tool for handling and cleaning data in various data analysis tasks.
Pandas: Replace Empty Strings with NaN
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:
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 positionpoints 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 127Notice that each of the empty strings have been replaced with NaN.
Note: You can find the complete documentation for the replace function in pandas .
Additional Resources
The following tutorials explain how to perform other common tasks in pandas:
Cite this article
stats writer (2024). How can empty strings be replaced with NaN in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-empty-strings-be-replaced-with-nan-in-pandas/
stats writer. "How can empty strings be replaced with NaN in Pandas?." PSYCHOLOGICAL SCALES, 30 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-empty-strings-be-replaced-with-nan-in-pandas/.
stats writer. "How can empty strings be replaced with NaN in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-empty-strings-be-replaced-with-nan-in-pandas/.
stats writer (2024) 'How can empty strings be replaced with NaN in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-empty-strings-be-replaced-with-nan-in-pandas/.
[1] stats writer, "How can empty strings be replaced with NaN in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can empty strings be replaced with NaN in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
