How can I add a row to an empty DataFrame in Pandas?

How can I add a row to an empty DataFrame in Pandas?

To add a row to an empty DataFrame in Pandas, you can use the `append()` function. This function takes in the row you want to add as a list or dictionary, and then appends it to the bottom of the DataFrame. Alternatively, you can also use the `loc` function to add a new row at a specific index position. Both of these methods will allow you to add a row to an empty DataFrame in Pandas.

Pandas: Add Row to Empty DataFrame


You can use the following basic syntax to add a row to an empty pandas DataFrame:

#define row to add
some_row = pd.DataFrame([{'column1':'value1', 'column2':'value2'}])

#add row to empty DataFrame
df = pd.concat([df, some_row])

The following examples show how to use this syntax in practice.

Example 1: Add One Row to Empty DataFrame

The following code shows how to add one row to an empty pandas DataFrame:

import pandas as pd

#create empty DataFrame
df = pd.DataFrame()

#define row to add
row_to_append = pd.DataFrame([{'team':'Mavericks', 'points':'31'}])

#add row to empty DataFrame
df = pd.concat([df, row_to_append])

#view updated DataFrame
print(df)

        team points
0  Mavericks     31

Notice that we created an empty DataFrame by using pd.DataFrame(), then added one row to the DataFrame by using the concat() function.

Example 2: Add Multiple Rows to Empty DataFrame

The following code shows how to add multiple rows to an empty pandas DataFrame:

import pandas as pd

#create empty DataFrame
df = pd.DataFrame()

#define rows to add
rows_to_append = pd.DataFrame([{'team':'Mavericks', 'points':'31'},
                               {'team':'Hawks', 'points':'20'},
                               {'team':'Hornets', 'points':'25'},
                               {'team':'Jazz', 'points':'43'}])

#add row to empty DataFrame
df = pd.concat([df, rows_to_append])

#view updated DataFrame
print(df)

        team points
0  Mavericks     31
1      Hawks     20
2    Hornets     25
3       Jazz     43

Once again we created an empty DataFrame by using pd.DataFrame(), then added multiple rows to the DataFrame by using the concat() function.

Note: You can find the complete documentation for the pandas concat() function . 

The following tutorials explain how to perform other common tasks in pandas:

Cite this article

stats writer (2024). How can I add a row to an empty DataFrame in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-add-a-row-to-an-empty-dataframe-in-pandas/

stats writer. "How can I add a row to an empty DataFrame in Pandas?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-add-a-row-to-an-empty-dataframe-in-pandas/.

stats writer. "How can I add a row to an empty DataFrame in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-add-a-row-to-an-empty-dataframe-in-pandas/.

stats writer (2024) 'How can I add a row to an empty DataFrame in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-add-a-row-to-an-empty-dataframe-in-pandas/.

[1] stats writer, "How can I add a row to an empty DataFrame in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I add a row to an empty DataFrame in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top