Home > Backend Development > Python Tutorial > How to Replace Blank Values with NaN in Pandas DataFrames?

How to Replace Blank Values with NaN in Pandas DataFrames?

DDD
Release: 2024-10-30 15:24:03
Original
632 people have browsed it

How to Replace Blank Values with NaN in Pandas DataFrames?

Replacing Blank Values with NaN in Pandas

In Pandas dataframes, it is often necessary to identify and replace whitespace values (" ") with NaN values. This operation ensures data integrity and enables seamless analysis.

One efficient way to accomplish this is through the Pandas replace() method. Its syntax is as follows:

<code class="python">df.replace(to_replace, value, regex=True)</code>
Copy after login

In this context:

  • to_replace is the regular expression pattern representing whitespace values (e.g., r'^s ').
  • value is the desired replacement value, which can be NaN.
  • regex is set to True to enable regular expression matching.

To replace blank values with NaN, you can use the following code:

<code class="python">import pandas as pd
import numpy as np

# Create a sample dataframe
df = pd.DataFrame({
    "A": [-0.532681, 1.490752, -1.387326, 0.814772, -0.222552, -1.176781],
    "B": ['foo', 'bar', 'foo', 'baz', ' ','qux'],
    "C": [0, 1, 2, ' ', 4, ' ']
})

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

# Display the updated dataframe
print(df)</code>
Copy after login

Output:

          A     B     C
0 -0.532681   foo     0
1  1.490752   bar     1
2 -1.387326   foo     2
3  0.814772   baz   NaN
4 -0.222552   NaN     4
5 -1.176781   qux   NaN
Copy after login

This code effectively replaces all blank values in the dataframe with NaN, providing a cleaner and more accurate representation of your data.

The above is the detailed content of How to Replace Blank Values with NaN in Pandas DataFrames?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template