Pandas Dataframe: How to add Rows & Columns

Add a new row and column to Pandas dataframe

Adding rows and columns in Pandas Dataframe is a very easy process. While working on a data project using Python programming, there are several scenarios when you’ll need to add new rows and columns to your Dataframe. In this article, we will show you how to do it. As data scientists or data analysts, you must get a good understanding of how to add Dataframe rows and columns.

In this post, we will work with the following Pandas data frame.

import pandas as pd

df = pd.DataFrame({
    "Mathematics": [95, 99],
    "Science": [98, 94]
}, index=["Aiyana", "Anisha"])

df

How to add a row in Dataframe

There are multiple ways of adding rows to Dataframe. You can use Dataframe.loc or Dataframe.append method to add a row at the end of Dataframe. If you want to insert a row at any specific position, then you can use Dataframe.insert() method. Let’s see all these methods one by one with an example.

Method 1: Add a new row at the End using loc method

In this method, we will take the help of the Dataframe.loc method which is used to access a group of rows and columns by label(s). If you want to add a single row to your Dataframe, you can use the .loc[] indexing method. The .loc[] method is used to access Dataframe elements by label, and it supports adding new rows as well as creating copies of existing ones. The following code represents how to add a Dataframe row at the end.

# Use loc method to add a new row with label
#
df.loc["Saanvi"] = [96, 90]

Method 2: Add a new row at the End using append method

In this method, we will take the help of Dataframe.append() method. Dataframe.append() is used to append rows of other Dataframes to the end of this Dataframe, returning a new object. Rows are added at the bottom, so the index labels are increasing, and duplicate index values are not preserved. The code below represents the same:

# Append one or more rows of another dataframe
#
df1 = pd.DataFrame({
    "Mathematics": [92],
    "Science": [95]
}, index=["Snehal"])
#
# Append a dataframe
#
df = df.append(df1)

Method 3: Add new rows at the End using concat method

The concat method can be used to add rows to a Pandas Dataframe. The concat method takes an iterable of Series or Dataframe objects and concatenates them into a single Dataframe. The concat method can be used to combine two or more Dataframes into a single Dataframe, or to combine a Series and a Dataframe into a single Dataframe. The following code represents way to add one or more rows to the end of Dataframe.

import pandas as pd
#
# Create a dataframe
#
dict = {"Mathematics":[95, 90, 99],
        "Science": [99, 95, 92]}

df1 = pd.DataFrame(dict, index=["Aiyana", "Anisha", "Saanvi"])
#
# Create another dataframe
#
df2 = pd.DataFrame({"Mathematics": [96],
                    "Science": [99]},
                   index=["Snehal"])
#
# Concat dataframes
#
pd.concat([df1, df2])

How to add a column in Dataframe

There are multiple ways of adding columns to Dataframe. You can use Dataframe.loc or bracket method to add a new column at the end of Dataframe. Let’s see all these methods one by one with an example.

Method 1: Add a new Column at the End using loc method

If you want to add a single column to your Dataframe, you can use the .loc[] indexing method. The .loc[] method is used to access Dataframe elements by label, and it supports adding new columns as well as creating copies of existing ones. The following code represents how to add a Dataframe column at the end.

# Adding a new column using loc method
#
df.loc[:, ["English"]] = [85, 92, 79, 87]

Method 2: Add a new Column at the End using brackets

In this method, we will take the help of using brackets on data frame object to insert a new column. The column is inserted at the end of all the columns. The following code represents how to add columns using brackets.

# Adding a new column using brackets
#
df["Hindi"] = [81, 79, 72, 76]

Method 3: Add a new Column at the End using insert method

In this method, we will take the help of Dataframe.insert() method. Dataframe.insert() is used to insert a column in Dataframe at a specified location. The column is inserted at the given position among all columns. The following code represents how to add a Dataframe column using Dataframe.insert() method:

# Adding a column at a specified position using insert method
#
df.insert(1, "Social Science", [86, 78, 82, 80])

Conclusion

Adding rows and columns to Dataframe is a very easy process. In this article, we have shown you different ways in which you can add new rows and columns to your Dataframe. We have also provided examples for each method so that you can understand it better. So, go ahead and try these methods out while working on your data projects using Python programming.

Ajitesh Kumar
Follow me
Latest posts by Ajitesh Kumar (see all)

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking
Posted in Data Science, Python. Tagged with , .

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.