python

Add new row to an existing Pandas DataFrame

If you have a pandas DataFrame and want to add a new row, there are various ways to do this. You can use the append method to add a new row to the end of the DataFrame. You can also use the insert method to add a new row at a specific index. If you want to add multiple rows at once, you can use the extend method.

Problem Statement

Let's say we have a DataFrame that contains multiple columns and rows. Ans we want to add one or multiple rows to this DataFrame then we can use one of the solutions explained in this post. We can create the DataFrame as below.

import pandas as pd

# create a dataframe
df = pd.DataFrame({
    'product_name': ['p1', 'p2', 'p3', 'p4'],
    'price': [100.0, 30.0, 200.0, 140.0],
    'quantity': [14, 67, 90, 40]
});
╒════╤════════════════╤═════════╤════════════╕
│    │ product_name   │   price │   quantity │
╞════╪════════════════╪═════════╪════════════╡
│  0 │ p1             │     100 │         14 │
├────┼────────────────┼─────────┼────────────┤
│  1 │ p2             │      30 │         67 │
├────┼────────────────┼─────────┼────────────┤
│  2 │ p3             │     200 │         90 │
├────┼────────────────┼─────────┼────────────┤
│  3 │ p4             │     140 │         40 │
╘════╧════════════════╧═════════╧════════════╛

Solution 1: Use concat() function to add new rows to the existing DataFrame

The pandas.concat() function is used to add new rows to an existing DataFrame. This is useful when you have data in different formats that you want to combine into a single DataFrame. For example, you may have data in CSV format and data in JSON format that you want to combine into a single DataFrame.

Code example

import pandas as pd

# create a dataframe
df_1 = pd.DataFrame({
    'product_name': ['p1', 'p2', 'p3', 'p4'],
    'price': [100.0, 30.0, 200.0, 140.0],
    'quantity': [14, 67, 90, 40]
});

# create another DataFrame
new_rows_data = {"product_name": ['p5'], 'price': [340.0], 'quantity': [70]}

# create new DataFrame from the above dictionary
df_2 = pd.DataFrame(new_rows_data)

df = pd.concat([df_1, df_2], ignore_index = True)

print(df)

Output

╒════╤════════════════╤═════════╤════════════╕
│    │ product_name   │   price │   quantity │
╞════╪════════════════╪═════════╪════════════╡
│  0 │ p1             │     100 │         14 │
├────┼────────────────┼─────────┼────────────┤
│  1 │ p2             │      30 │         67 │
├────┼────────────────┼─────────┼────────────┤
│  2 │ p3             │     200 │         90 │
├────┼────────────────┼─────────┼────────────┤
│  3 │ p4             │     140 │         40 │
├────┼────────────────┼─────────┼────────────┤
│  4 │ p5             │     340 │         70 │
╘════╧════════════════╧═════════╧════════════╛
  1. The above code first creates a DataFrame called df_1.
  2. This DataFrame df_1 consists of 4 rows, where each row has 3 columns: product_name, price, and quantity.
  3. Then, the code creates another DataFrame called df_2, which has 1 row with the same 3 columns.
  4. Finally, the code concatenates these 2 DataFrames into 1 DataFrame called df.
  5. The resultant DataFrame df has 5 rows and 3 columns.

Solution 2: Add a python dictionary as a row using the append() function

In Python, a DataFrame is a two-dimensional data structure, similar to a table in a relational database. You can add data to a DataFrame in various ways, but one common method is to use the append() function.

The append() function takes a single argument, which is a dictionary. The keys in the dictionary become the columns in the DataFrame, and the values become the rows. For example, if you have a dictionary with the keys "a" and "b" and the values 1 and 2, the resulting DataFrame will have two columns (a and b) and one row (1, 2).

You can append multiple dictionaries to a DataFrame by passing a list of dictionaries to the append() function. The resulting DataFrame will have the same number of columns as the original DataFrame, but the number of rows will be equal to the sum of the number of rows in the original DataFrame and the number of dictionaries passed to the append() function.

In summary, the append() function is a convenient way to add data to a DataFrame. It can take a dictionary as an argument, and the keys in the dictionary become the columns in the resulting DataFrame.

Code example

import pandas as pd

# create a dataframe
df = pd.DataFrame({
    'product_name': ['p1', 'p2', 'p3', 'p4'],
    'price': [100.0, 30.0, 200.0, 140.0],
    'quantity': [14, 67, 90, 40]
});

row_dict = {"product_name": 'p5', 'price': 340.0, 'quantity': 70}

# add a new row to df DataFrame
df = df.append(row_dict, ignore_index=True)

print(df)

Output

╒════╤════════════════╤═════════╤════════════╕
│    │ product_name   │   price │   quantity │
╞════╪════════════════╪═════════╪════════════╡
│  0 │ p1             │     100 │         14 │
├────┼────────────────┼─────────┼────────────┤
│  1 │ p2             │      30 │         67 │
├────┼────────────────┼─────────┼────────────┤
│  2 │ p3             │     200 │         90 │
├────┼────────────────┼─────────┼────────────┤
│  3 │ p4             │     140 │         40 │
├────┼────────────────┼─────────┼────────────┤
│  4 │ p5             │     340 │         70 │
╘════╧════════════════╧═════════╧════════════╛

You may see the below warning when executing the above code.

FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.

Solution 3: Using DataFrame.loc[] method

Adding a new row to a pandas DataFrame can be done using the loc[] method. This method allows you to specify the location of the new row, as well as the data that will go in that row. In this example, we will add a new row at the end of the DataFrame.

Code example

import pandas as pd

# create a dataframe
df = pd.DataFrame({
    'product_name': ['p1', 'p2', 'p3', 'p4'],
    'price': [100.0, 30.0, 200.0, 140.0],
    'quantity': [14, 67, 90, 40]
});

# add new row using df.loc[] method
df.loc[len(df.index)] = ['p5', 300.0, 35]

print(df)

Output

╒════╤════════════════╤═════════╤════════════╕
│    │ product_name   │   price │   quantity │
╞════╪════════════════╪═════════╪════════════╡
│  0 │ p1             │     100 │         14 │
├────┼────────────────┼─────────┼────────────┤
│  1 │ p2             │      30 │         67 │
├────┼────────────────┼─────────┼────────────┤
│  2 │ p3             │     200 │         90 │
├────┼────────────────┼─────────┼────────────┤
│  3 │ p4             │     140 │         40 │
├────┼────────────────┼─────────┼────────────┤
│  4 │ p5             │     300 │         35 │
╘════╧════════════════╧═════════╧════════════╛

Explanation of the above code example

  1. The first line imports the pandas library into the file.
  2. The second line creates a DataFrame object called df. This object has three columns: product_name, price, and quantity.
  3. The third line adds a new row to the DataFrame using the df.loc[] method. This method takes two arguments: the first is the index of the row to be added, and the second is a list containing the values for each column in the row. In this case, the index of the new row is the length of the df.index object (which is the number of rows in the DataFrame), and the values are ['p5', 300.0, 35].
  4. The fourth line prints the DataFrame to the console. 
Was this helpful?