Replacing blank values (white space) with NaN in pandas

Replacing blank values (white space) with NaN in pandas

You can replace blank values (white space) with NaN (Not-a-Number) in a Pandas DataFrame using the replace() method or by directly using the numpy.nan constant. Here's how you can do it:

Method 1: Using the replace() method:

import pandas as pd # Create a sample DataFrame with blank values data = {'A': ['foo', 'bar', ' ', 'baz'], 'B': [1, 2, ' ', 4]} df = pd.DataFrame(data) # Replace blank values with NaN using the replace() method df = df.replace(' ', pd.NA) # df now contains NaN instead of blank values print(df) 

In this example, we use the replace() method to replace all occurrences of blank values with pd.NA, which represents NaN in Pandas.

Method 2: Using numpy.nan:

import pandas as pd import numpy as np # Create a sample DataFrame with blank values data = {'A': ['foo', 'bar', ' ', 'baz'], 'B': [1, 2, ' ', 4]} df = pd.DataFrame(data) # Replace blank values with NaN using numpy.nan df = df.replace(' ', np.nan) # df now contains NaN instead of blank values print(df) 

In this method, we use numpy.nan from the NumPy library to replace blank values with NaN. Both methods will give you the same result, and the DataFrame df will have NaN values in place of the blank values.

Examples

  1. How to replace blank values with NaN in a pandas DataFrame?

    • Use replace with a regular expression to replace blank values with NaN.
    import pandas as pd import numpy as np df = pd.DataFrame({ 'A': ['foo', ' ', 'bar'], 'B': [1, ' ', 3] }) df.replace(r'^\s*$', np.nan, regex=True, inplace=True) # df is now: # A B # foo 1.0 # NaN NaN # bar 3.0 
  2. How to replace blank values in a pandas DataFrame column with NaN?

    • Use the str.strip() method to find blank values and replace them with NaN.
    df = pd.DataFrame({ 'Name': ['Alice', ' ', 'Bob'], 'Age': [25, 30, 35] }) df['Name'] = df['Name'].str.strip().replace('', np.nan) # df is now: # Name Age # Alice 25 # NaN 30 # Bob 35 
  3. How to replace blank values in a pandas Series with NaN?

    • Use the replace method with a regular expression to replace blank spaces with NaN.
    series = pd.Series(['hello', ' ', 'world']) series = series.replace(r'^\s*$', np.nan, regex=True) # series is now: # 0 hello # 1 NaN # 2 world # dtype: object 
  4. How to replace blank values with NaN in multiple DataFrame columns?

    • Use apply with replace to replace blank values across multiple columns.
    df = pd.DataFrame({ 'A': ['x', ' ', 'z'], 'B': ['y', ' ', 'x'] }) df = df.apply(lambda x: x.replace(r'^\s*$', np.nan, regex=True)) # df is now: # A B # x y # NaN NaN # z x 
  5. How to replace empty strings with NaN in a pandas DataFrame?

    • Use replace to change empty strings into NaN.
    df = pd.DataFrame({ 'A': ['', 'foo', 'bar'], 'B': [1, '', 3] }) df.replace('', np.nan, inplace=True) # df is now: # A B # NaN 1.0 # foo NaN # bar 3.0 
  6. How to replace blank values with NaN in pandas DataFrame using list comprehension?

    • Use list comprehension to identify blank values and replace them with NaN.
    df = pd.DataFrame({ 'A': [' ', 'foo', 'bar'], 'B': [1, 2, ' '] }) df = df.applymap(lambda x: np.nan if isinstance(x, str) and x.strip() == '' else x) # df is now: # A B # NaN 1.0 # foo 2.0 # bar NaN 
  7. How to replace blank values in a pandas DataFrame with NaN using custom function?

    • Define a custom function to identify and replace blank values with NaN.
    def replace_blank(value): if isinstance(value, str) and value.strip() == '': return np.nan return value df = pd.DataFrame({ 'Item': ['item1', ' ', 'item3'], 'Quantity': [1, ' ', 3] }) df = df.applymap(replace_blank) # df is now: # Item Quantity # item1 1.0 # NaN NaN # item3 3.0 
  8. How to replace blank values in a pandas DataFrame by column type with NaN?

    • Apply replace with a condition to specific column types.
    df = pd.DataFrame({ 'Name': ['Alice', ' ', 'Bob'], 'Age': [25, '', 30] }) df['Name'] = df['Name'].str.strip().replace('', np.nan) df['Age'] = df['Age'].replace('', np.nan) # df is now: # Name Age # Alice 25.0 # NaN NaN # Bob 30.0 
  9. How to replace blank values with NaN in pandas without regex?

    • Use conditional assignment to replace blank values with NaN without regex.
    df = pd.DataFrame({ 'Name': [' ', 'John', 'Jane'], 'Age': [25, ' ', 35] }) df['Name'] = df['Name'].str.strip() df['Name'][df['Name'] == ''] = np.nan df['Age'] = df['Age'].apply(lambda x: np.nan if x == ' ' else x) # df is now: # Name Age # NaN 25 # John NaN # Jane 35 

More Tags

recursion geometry oracle-apex wear-os background intersection kramdown callback axios iis-7.5

More Python Questions

More Biology Calculators

More Electronics Circuits Calculators

More Retirement Calculators

More Fitness-Health Calculators