Pandas merge fill NAN with 0 in Python

Do you want to merge fill NAN values with 0 in a dataframe? In this Python Tutorial, I will explain how Pandas merge fill NAN with 0 in Python using different methods with some illustrative examples.

To fill NaN values with 0 in Python using Pandas, methods like df.fillna() and df.replace() are employed. These methods effectively merge and fill NaN values with 0, streamlining data preprocessing by ensuring the datasets are clean and uniform for further analysis.

Pandas merge fill NAN with 0 in Python

There are two different methods to merge fill the NAN values with 0 in Python Pandas dataframes:

  1. df.fillna() function
  2. df.replace() function

Let’s see them one by one using some illustrative examples:

1. Pandas merge nan to 0 using df.fillna() function

The df.fillna() function in Pandas is used to replace NaN (Not a Number) values in a DataFrame or Series with a specified value. It is particularly useful for handling missing data.

Case 1: Pandas replace nan with 0 for a column using df.fillna() function

Suppose we have a DataFrame with several columns, and we only want to replace NaN values in one specific column, we can use the df.fillna() function in Pandas with the name of that column.

READ:  Matplotlib set_xticks - Detailed tutorial

Here is the code, Pandas merge fill NAN with 0 in Python using df.fillna() function:

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'City': ['New York', 'Los Angeles', 'Chicago', 'Houston'],
    'Temperature': [59, np.nan, 48, np.nan],
    'Humidity': [55, 60, np.nan, 70]
})
print("Before Merge fill:\n", df)

df['Temperature'].fillna(0, inplace=True)
print("After Merge fill:\n", df)

Output:

Before Merge fill:
           City  Temperature  Humidity
0     New York         59.0      55.0
1  Los Angeles          NaN      60.0
2      Chicago         48.0       NaN
3      Houston          NaN      70.0
After Merge fill:
           City  Temperature  Humidity
0     New York         59.0      55.0
1  Los Angeles          0.0      60.0
2      Chicago         48.0       NaN
3      Houston          0.0      70.0

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

Pandas merge fill NAN with 0 in Python

Case 2: fill nan with 0 Pandas for a dataframe using df.fillna() function

Let’s replace NaN values in all columns of the DataFrame using the df.fillna() function.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'Patient_ID': [101, 102, 103, 104],
    'Blood Pressure': [120, np.nan, 115, np.nan],
    'Cholesterol': [200, 220, np.nan, 180]
})
print("Before Merge fill:\n", df)

df.fillna(0, inplace=True)
print("After Merge fill:\n", df)

Output:

Before Merge fill:
    Patient_ID  Blood Pressure  Cholesterol
0         101           120.0        200.0
1         102             NaN        220.0
2         103           115.0          NaN
3         104             NaN        180.0
After Merge fill:
    Patient_ID  Blood Pressure  Cholesterol
0         101           120.0        200.0
1         102             0.0        220.0
2         103           115.0          0.0
3         104             0.0        180.0

After executing the code in Pycharm, one can see the output in the below screenshot.

pandas fill nan with 0 in Python

2. Python nan to zero in a Pandas dataframe using df.replace() function

The df.replace() function in Pandas, offers more general functionality, allowing the replacement of a variety of values (not just NaN) with either another value or a list of values. It provides a broader scope for value substitution within a DataFrame or Series.

READ:  Matplotlib best fit line

Case 1: Replace nan with 0 Pandas for a column using df.replace() function

We want to replace NaN values only in a column of a dataframe in Python, with the specified column name in the df.replace() function.

Here is the use of the df.replace() function by Pandas merge fill NAN with 0 in Python:

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'Applicant_ID': ['A001', 'A002', 'A003', 'A004'],
    'Annual Income': [50000, np.nan, 75000, np.nan],
    'Credit Score': [700, 650, np.nan, 720]
})
print("Before Merge fill:\n", df)

df['Annual Income'].replace(np.nan, 0, inplace=True)
print("After Merge fill:\n", df)

Output:

Before Merge fill:
   Applicant_ID  Annual Income  Credit Score
0         A001        50000.0         700.0
1         A002            NaN         650.0
2         A003        75000.0           NaN
3         A004            NaN         720.0
After Merge fill:
   Applicant_ID  Annual Income  Credit Score
0         A001        50000.0         700.0
1         A002            0.0         650.0
2         A003        75000.0           NaN
3         A004            0.0         720.0

Below is a screenshot showcasing the output after executing the code in the Pycharm editor.

pandas merge replace nan with 0 in Python

Case 2: Python replace nan with 0 for a dataframe using df.replace() function

For the dataframe in Pandas merge fill NAN with 0 in Python from all columns, we can use the df.replace() function.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'State': ['California', 'Texas', 'Florida', 'New York'],
    'Rainfall': [np.nan, 28, np.nan, 41],
    'Population_Millions': [39.5, 29, 21.5, 19.5]
})
print("Before Merge fill:\n", df)

df.replace(np.nan, 0, inplace=True)
print("After Merge fill:\n", df)

Output:

Before Merge fill:
         State  Rainfall  Population_Millions
0  California       NaN                 39.5
1       Texas      28.0                 29.0
2     Florida       NaN                 21.5
3    New York      41.0                 19.5
After Merge fill:
         State  Rainfall  Population_Millions
0  California       0.0                 39.5
1       Texas      28.0                 29.0
2     Florida       0.0                 21.5
3    New York      41.0                 19.5

The following screenshot, taken after implementing the code in Pycharm, displays the result.

pandas merge fill nan with 0 with replace function in Python

Conclusion

Here, I have explained how Pandas merge fill NAN with 0 in Python, specifically focusing on two powerful methods: the df.fillna() function and the df.replace() function with different examples.

READ:  Matplotlib plot a line (Detailed Guide)

These techniques offer efficient ways to replace NaN values with 0, either in specific columns or across entire DataFrames, ensuring clean and consistent datasets for analysis.

You may also like to read: