Pandas combine two columns with null values

Given a pandas dataframe, we have to combine two columns with null values.
Submitted by Pranit Sharma, on October 12, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

While creating a DataFrame or importing a CSV file, there could be some NaN values in the cells. NaN values mean "Not a Number" which generally means that there are some missing values in the cell.

Problem statement

Suppose we are given a DataFrame with two columns, these columns may contain some null values. We need to combine these two columns by ignoring null values. If both the columns have a null value for some row, we want the new column would also have null values at that particular point.

Combine two columns with null values

To combine two columns with null values, we will use the fillna() method for the first column and inside this method, we will pass the second column so that it will fill the none values with the values of the first column.

Let us understand with the help of an example,

Python program to combine two columns with null values

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating two dictionary
d = {
    'A':['Raftar', 'Remo', None, None, 'Divine'],
    'B':['Rap', None, 'Dance', None, None]
}

# Creating a DataFrame
df = pd.DataFrame(d)

# Display DataFrame
print("DataFrame1:\n",df,"\n")

# combining values
df['new'] = df['A'].fillna(df['B'])

# display result
print("Result:\n",df)

Output

The output of the above program is:

Example: Pandas combine two columns with null values

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.