15.1 C
London
Friday, July 5, 2024
HomePandas in PythonGeneral Functions in PythonHow to Add Rows to a Pandas DataFrame (With Examples)

How to Add Rows to a Pandas DataFrame (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the df.loc() function to add a row to the end of a pandas DataFrame:

#add row to end of DataFrame
df.loc[len(df.index)] = [value1, value2, value3, ...]

And you can use the df.append() function to append several rows of an existing DataFrame to the end of another DataFrame:

#append rows of df2 to end of existing DataFrame
df = df.append(df2, ignore_index = True)

The following examples show how to use these functions in practice.

Example 1: Add One Row to Pandas DataFrame

The following code shows how to add one row to the end of a pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'points': [10, 12, 12, 14, 13, 18],
                   'rebounds': [7, 7, 8, 13, 7, 4],
                   'assists': [11, 8, 10, 6, 6, 5]})

#view DataFrame
df

	points	rebounds assists
0	10	7	 11
1	12	7	 8
2	12	8	 10
3	14	13	 6
4	13	7	 6
5	18	4	 5

#add new row to end of DataFrame
df.loc[len(df.index)] = [20, 7, 5]

#view updated DataFrame
df

        points	rebounds assists
0	10	7	 11
1	12	7	 8
2	12	8	 10
3	14	13	 6
4	13	7	 6
5	18	4	 5
6	20	7	 5

Example 2: Add Several Rows to Pandas DataFrame

The following code shows how to add several rows of an existing DataFrame to the end of another DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'points': [10, 12, 12, 14, 13, 18],
                   'rebounds': [7, 7, 8, 13, 7, 4],
                   'assists': [11, 8, 10, 6, 6, 5]})

#view DataFrame
df

	points	rebounds assists
0	10	7	 11
1	12	7	 8
2	12	8	 10
3	14	13	 6
4	13	7	 6
5	18	4	 5

#define second DataFrame
df2 = pd.DataFrame({'points': [21, 25, 26],
                    'rebounds': [7, 7, 13],
                    'assists': [11, 3, 3]})

#add new row to end of DataFrame
df = df.append(df2, ignore_index = True)

#view updated DataFrame
df

        points	rebounds assists
0	10	7	 11
1	12	7	 8
2	12	8	 10
3	14	13	 6
4	13	7	 6
5	18	4	 5
6	21	7	 11
7	25	7	 3
8	26	13	 3

Note that the two DataFrames should have the same column names in order to successfully append the rows of one DataFrame to the end of another.

Additional Resources

How to Add a Column to a Pandas DataFrame
How to Get Row Numbers in a Pandas DataFrame
How to Convert a List to a DataFrame Row in Pandas

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories