• Post author:
  • Post category:Pandas
  • Post last modified:November 19, 2024
  • Reading time:18 mins read
You are currently viewing 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.

Advertisements

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 –

  • Use fillna('') to replace NaN values with an empty string in a DataFrame or Series.
  • The inplace=True parameter in fillna() allows modifying the DataFrame without creating a new copy.
  • Replace NaN with a blank string in specific columns by selecting them before applying fillna().
  • Apply fillna('') to the entire DataFrame to replace NaN in all columns at once.
  • The replace() function can also be used to replace NaN values with an empty string by specifying np.nan.
  • If you want to preserve the original DataFrame, avoid using inplace=True and assign the result to a new variable.
  • Use functions like .isna() or .isnull() to check for NaN values before replacing, ensuring that the operation targets the intended cells.

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.

Pandas NaN Empty String

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.

Pandas NaN Empty String

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

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

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         

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

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

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

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

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

FAQ on Pandas Replace NaN with Blank/Empty String

How can I replace NaN with a blank string in a Pandas Series?

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

How do I replace NaN with a blank string in a Pandas DataFrame?

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

What if I want to replace NaN only in specific columns?

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

What is the difference between replacing NaN with None vs. an empty string?

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.

Can I replace NaN with a blank string in a Series with mixed data types?

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.

Can I replace NaN with a specific string instead of an empty string?

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.

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

References

Leave a Reply