Missing data, insert rows in Pandas and fill with NAN

Given a pandas dataframe, we have to insert rows in pandas and fill with NaN values.
Submitted by Pranit Sharma, on October 20, 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.

Problem statement

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.

We are given a DataFrame of discontinuous data, we need to find a solution so that we can add new rows and fill them with NaN values.

Inserting rows in Pandas and fill with NAN

For this purpose, we will use the set_index() method and the reset_index() method. First, we will move a column to index, then we will add some rows and fill the values with nan, and then we will again move back the column.

Let us understand with the help of an example,

Python program to insert rows in Pandas and fill with NAN

# Importing pandas package
import pandas as pd

# Creating a dictionary
d = {
    "A":[0,0.5,1.0,3.5,4.0,4.5],
    "B":[1,4,6,2,4,3],
    "C":[3,2,1,0,5,3]
}

# Creating DataFrame
df = pd.DataFrame(d)

# Display original DataFrames
print("Original Dataframe :\n",df,"\n")

# setting index
df.set_index("A")

# Creating new index values
new_index = pd.Index((0,5,0.5), name="A")

# Setting index for A and then 
# resetting the index
df.set_index("A").reindex(new_index)

# Getting the result
res = df.set_index("A").reindex(new_index).reset_index()

# Display new DataFrame
print("New DataFrame:\n",res)

Output

Example: Missing data, insert rows in Pandas and fill with NAN

Python Pandas Programs »


Comments and Discussions!

Load comments ↻






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