Append an empty row in dataframe using pandas

Given a pandas dataframe, we have to append an empty row in it.
Submitted by Pranit Sharma, on September 06, 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.

Appending an empty row in pandas dataframe

We will first create a DataFrame and then we will add an empty row by using the concat() method or append() method, inside this method we will pass an empty Series such that it does not hold any value.

Adding a series will add 1 null value to each column and hence we will end up adding an extra row with the null value.

Rows in pandas are the different cell (column) values that are aligned horizontally and also provide uniformity. Each row can have the same or different value. Rows are generally marked with the index number but in pandas, we can also assign index names according to the needs. In pandas, we can create, read, update, and delete a column or row value.

Let us understand with the help of an example,

Python code to append an empty row in dataframe

# Importing pandas package
import pandas as pd

# Creating a Dictionary
d = {
    'Product':['TV','Fridge','AC'],
    'Electronic':[True,False,False],
    'Eletric':[False,True,True]
}

# Creating DataFrame
df = pd.DataFrame(d)

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

# Creating an empty series
s = pd.Series([None,None,None],index=['Product','Electronic','Electric'])

# Appending empty series to df
result = df.append(s,ignore_index=True)

# Display modified DataFrame
print("Modified DataFrame:\n",result)

Output

The output of the above program is:

Example: Append an empty row in dataframe

To understand the above example, you should have the knowledge of the following Python topics:

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





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