Built with Tutify.io

    AIStudyLabs

    Pandas Replace NaN with Blank/Empty String

    Pandas Replace NaN with Blank/Empty String

    By using replace() or fillna() methods you can replace NaN values with Blank/Empty string in Pandas DataFrame. NaN stands for Not A Nuber and is one of the common ways to represent the missing data value in Python/Pandas DataFrame. Sometimes we would be required to convert/replace any missing values with the values that make sense like replacing with zero's for numeric columns and blank or empty for string-type columns.

    In this pandas DataFrame article, I will explain how to convert single or multiple (all columns from the list) NaN columns values to blank/empty strings using several ways with examples.

    Key Points –

    <a id="h-quick-examples-of-replace-nan-to-empty-blank-string"></a>

    Quick Examples of Replace NaN to Empty/Blank String

    If you are in a hurry, below are some quick examples of how to replace NaN with a blank/empty string in Pandas DataFrame.

    
    # Quick examples of replace nan to empty/blank string
    
    # Replace all Nan values to empty string
    df2 = df.replace(np.nan, '', regex=True)
    
    # Using multiple columns 
    df2 = df[['Courses','Fee' ]] = df[['Courses','Fee' ]].fillna('')
    
    # Using pandas.DataFrame.fillna() 
    # To replace nan values 
    df2 = df.fillna("")
    
    # Using pandas replace nan with null 
    df2 = df.fillna('', inplace=True)
    
    # Pandas single column using replace nan empty string 
    df2 = df.Courses.fillna('')
    
    # Using Courses column replace nan with Zeros
    df2 = df['Courses']=df['Courses'].fillna(0)
    
    # Using Discount column to replace nan with zeros
    df2 = df['Discount']=df['Discount'].fillna(0)
    
    # Remove the nan and fill the empty string
    df2 = df.Courses.replace(np.nan,'',regex = True)
    
    # Remove the nan and fill some values
    df2 = df.Courses.replace(np.nan,'value',regex = True)
    

    Now, let's create a DataFrame with a few rows and columns and execute some examples, and validate the results. Our DataFrame contains column names Courses, Fee, Duration and Discount.

    
    import pandas as pd
    import numpy as np
    
    technologies = {
        'Courses':["Spark",np.nan,"Hadoop","Python","pandas",np.nan,"Java"],
        'Fee' :[20000,25000, np.nan,22000,24000,np.nan,22000],
        'Duration':[np.nan,'40days','35days', np.nan,'60days','50days','55days'],
        'Discount':[1000,np.nan,1500,np.nan,2500,2100,np.nan]
                  }
    df = pd.DataFrame(technologies)
    print("Create DataFrame:\n", df)
    

    Yields below output.

    <a id="h-convert-nan-to-empty-string-in-pandas"></a>

    Convert Nan to Empty String in Pandas

    Use df.replace(np.nan,'',regex=True) method to replace all NaN values with an empty string in the Pandas DataFrame column.

    
    # All DataFrame replace empty string
    df2 = df.replace(np.nan, '', regex=True)
    print("After replacing the NaN values with an empty string:\n", df2)
    

    Yields below output.

    Related: You can also replace an empty/blank string with NaN values.

    <a id="h-multiple-columns-replace-empty-string"></a>

    Multiple Columns Replace Empty String

    In order to replace NaN values with Blank strings on multiple columns or all columns from a list, use df[['Courses','Fee']] = df[['Courses','Fee']].fillna(''). This replaces NaN values on Courses and Fee column.

    
    # Using multiple columns 
    df2 = df[['Courses','Fee' ]] = df[['Courses','Fee' ]].fillna('')
    print("After replacing the NaN values with an empty string:\n", df2)
    

    Yields below output.

    
    # Output:
    # After replacing the NaN values with an empty string:
      Courses      Fee
    0   Spark  20000.0
    1          25000.0
    2  Hadoop         
    3  Python  22000.0
    4  pandas  24000.0
    5                 
    6    Java  22000.0
    

    <a id="h-using-fillna-to-nan-null-values-with-empty-string"></a>

    Using fillna() to NaN/Null Values With Empty String

    Use pandas.DataFrame.fillna() to Replace NaN/Null values with an empty string. This replaces each NaN in Pandas DataFrame with an empty string.

    
    # Using pandas.DataFrame.fillna() 
    # To nan values 
    df2 = df.fillna("")
    print("After replacing the NaN values with an empty string:\n", df2)
    

    Yields below output.

    
    # Output:
    # After replacing the NaN values with an empty string:
      Courses      Fee Duration Discount
    0   Spark  20000.0            1000.0
    1          25000.0   40days         
    2  Hadoop            35days   1500.0
    3  Python  22000.0                  
    4  pandas  24000.0   60days   2500.0
    5                    50days   2100.0
    6    Java  22000.0   55days
    

    <a id="h-fillna-with-inplace-true"></a>

    fillna() with inplace=True

    If you notice the above output after applying fillna() function, it returns a new DataFrame, In order to update the current/referring DataFrame in place use df.fillna('',inplace=True). When using this, fillna() method returns None type.

    
    # Using pandas replace nan with null
    df2 = df.fillna('', inplace=True)
    print("After replacing the NaN values with an empty string:\n", df2)
    

    Yields below output.

    
    # Output:
    None
    

    <a id="h-replacing-nan-with-empty-string-on-a-specific-column"></a>

    Replacing NaN with Empty String on a Specific Column

    If you want to fill a single column, you can use df.Courses.fillna('').

    
    # Pandas single column 
    # Using replace nan empty string 
    df2 = df.Courses.fillna('')
    print("After replacing the NaN values with an empty string:\n", df2)
    

    Yields below output.

    
    # Output:
    # After replacing the NaN values with an empty string:
    0     Spark
    1          
    2    Hadoop
    3    Python
    4    pandas
    5          
    6      Java
    Name: Courses, dtype: object
    

    <a id="h-replace-nan-with-zeros"></a>

    Replace NaN with Zeros

    These examples replace NaN values with zeroes in a column.

    
    # Using Courses column replace nan with Zeros
    df2 = df['Courses']=df['Courses'].fillna(0)
    print("After replacing the NaN values with zeros:\n", df2)
    
    # Using Discount column to replace nan with Zeros
    df2 = df['Discount']=df['Discount'].fillna(0)
    print("After replacing the NaN values with zeros:\n", df2)
    

    Yields below output.

    
    # Output:
    # After replacing the NaN values with zeros:
    0     Spark
    1         0
    2    Hadoop
    3    Python
    4    pandas
    5         0
    6      Java
    Name: Courses, dtype: object
    

    <a id="h-remove-the-nan-and-fill-the-empty-string"></a>

    Remove the NaN and Fill the Empty String

    Use df.Courses.replace(np.nan,'',regex=True) to remove the NaN and fill the empty string on a Courses column.

    
    # Remove the nan and fill the empty string
    df2 = df.Courses.replace(np.nan,'',regex = True)
    print("After replacing NaN values with an empty string:\n", df2)
    

    Yields below output.

    
    # Output:
    # After replacing NaN values with an empty string:
    0     Spark
    1          
    2    Hadoop
    3    Python
    4    pandas
    5          
    6      Java
    Name: Courses, dtype: object
    

    <a id="h-remove-the-nan-and-fill-some-values"></a>

    Remove the NaN and Fill Some Values

    Use df.Courses.replace(np.nan,'value',regex=True) to remove the NaN and fill Value.

    
    # Remove the nan and fill some values
    df2 = df.Courses.replace(np.nan,'value',regex = True)
    print("After replacing NaN values with specified value:\n", df2)
    

    Yields below output.

    
    # Output:
    # After replacing NaN values with specified value:
    0     Spark
    1     value
    2    Hadoop
    3    Python
    4    pandas
    5     value
    6      Java
    Name: Courses, dtype: object
    

    <a id="h-faq-on-pandas-replace-nan-with-blank-empty-string"></a>

    FAQ on Pandas Replace NaN with Blank/Empty String

    To replace NaN values with a blank string in a Pandas Series, you can use the fillna() method.

    You can apply fillna() to the entire DataFrame to replace NaN values with an empty string for all columns.

    You can replace NaN with an empty string in specific columns by selecting those columns.

    While both None and an empty string ("") are used to represent missing or undefined values, None is considered a NaN equivalent in numerical computations and keeps the column as the original data type.

    fillna() can handle Series with mixed data types, but replacing NaN values with blank strings will convert all values to the object data type, even if they were originally numeric.

    You can replace NaN values with a specific string instead of an empty string in a Pandas Series. You simply pass the desired string as an argument to the fillna() method.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, you have learned how to replace NaN with blank/empty strings in Pandas using DataFrame.fillna(), DataFrame.replace() functions, you have also learned how to replace single and multiple columns.

    Happy Learning !!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References