In this article, we will discuss how to add a row to a pandas DataFrame in Python. We will discuss:
- How to add a row at the bottom of a pandas DataFrame?
- How to add a row at the top of a pandas DataFrame?
- How to insert a row at a specific index of a pandas DataFrame?
How to add a row at the bottom of a pandas DataFrame using Python?
In the pandas Python library, we can add a row in a DataFrame using loc[]. For example,
import pandas list1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] df = pandas.DataFrame(list1) print("df before adding row: \n", df) df.loc[3] = [10, 11, 12] print("df after adding row: \n", df)
The output of the above program will be:
df before adding row: 0 1 2 0 1 2 3 1 4 5 6 2 7 8 9 df after adding row: 0 1 2 0 1 2 3 1 4 5 6 2 7 8 9 3 10 11 12
Here, we are first creating a DataFrame from the list list1. The DataFrame, at this point, has three rows and three columns. We …






0 Comments