Table of Contents
Rows can be added to a Pandas DataFrame by using the “append” function. This function takes in a new row of data and adds it to the bottom of the existing DataFrame. It is important to ensure that the new row has the same number and order of columns as the existing DataFrame. Additionally, the “concat” function can also be used to combine two DataFrames, thereby adding rows from one DataFrame to the other. Both of these methods allow for the efficient addition of new data to a Pandas DataFrame.
Add Rows to a Pandas DataFrame (With Examples)
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.
Cite this article
stats writer (2024). How can rows be added to a Pandas DataFrame?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-rows-be-added-to-a-pandas-dataframe/
stats writer. "How can rows be added to a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 2 May. 2024, https://scales.arabpsychology.com/stats/how-can-rows-be-added-to-a-pandas-dataframe/.
stats writer. "How can rows be added to a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-rows-be-added-to-a-pandas-dataframe/.
stats writer (2024) 'How can rows be added to a Pandas DataFrame?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-rows-be-added-to-a-pandas-dataframe/.
[1] stats writer, "How can rows be added to a Pandas DataFrame?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can rows be added to a Pandas DataFrame?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
