Python Pandas

Pandas Add Row to DataFrame

In Python, “DataFrames” are an essential component of data analysis. The “pandas” library offers several methods to work with DataFrames. Inserting or adding rows to a DataFrame is a fundamental approach when analyzing or manipulating data. Python provides various methods to insert rows in Pandas DataFrame.

This Python post presents a detailed tutorial on how to add/insert a row to Pandas DataFrame utilizing several examples.

How to Add/Insert a Row to Pandas DataFrame in Python?

To add/insert a row to Python Pandas DataFrame, the below methods are utilized in Python:

Method 1: Add/Insert a Row to Pandas DataFrame Using the “dataframe.loc[ ]” Method

The “df.loc[ ]” method adds a row to a Pandas DataFrame. This method allows the user to select a specific location within the DataFrame and assign values.

Example

Here is an example code:

import pandas

data1 = pandas.DataFrame({"students": ["Mary", "Queen", "Anna"], "marks": [52, 63, 84], "id_no": [5, 6, 2]})

print('Original DataFrame:\n',data1)

new_row = {'students': 'Emily', 'marks': 30, 'id_no': 8}

data1.loc[len(data1)] = new_row

print('\nAfter Adding Row to DataFrame:\n',data1)

In the above code:

  • The “pandas” library is imported and “DataFrame” with three columns is created.
  • The dictionary “new_row” is initialized in the program. This dictionary represents the values of the new row we want to add.
  • Finally, use the “df.loc[len(df)] = new_row” method to add a new row to the DataFrame. The “len(df)” ensures that the new row is included at the end/last of the DataFrame.

Output

The particular DataFrame has been updated with the new row.

Method 2: Add/Insert a Row to Pandas DataFrame Using the “pandas.concat()” Function

Another method to add a row to a Pandas DataFrame is via the “pd.concat()” function. This function concatenates two DataFrames along a particular axis.

Example

Here is an example code:

import pandas

data1 = pandas.DataFrame({"students": ["Mary", "Queen", "Anna"], "marks": [52, 63, 84], "id_no": [5, 6, 2]})

print('Original DataFrame:\n',data1)

new_row = pandas.DataFrame({'students': ['Emily'], 'marks': [30], 'id_no': [8]})

data1 = pandas.concat([data1, new_row], ignore_index=True)

print('\nAfter Adding Row to DataFrame:\n',data1)

In the above code lines:

  • The “pandas” library is imported, and “DataFrame” with three columns is created.
  • Now, likewise, define a new DataFrame named “new_row” that represents the values of the new row we want to add.
  • Finally, use the “pd.concat()” function to concatenate the “data1” DataFrame and “new_row” DataFrame along the rows axis (axis=0).
  • The “ignore_index=True” parameter ensures that the index of the resulting DataFrame is reset.

Output

The particular DataFrame has been updated with the new row.

Method 3: Add/Insert a Row to Pandas DataFrame Utilizing the “dataframe.append()” Function

The “dataframe.append()” function can also be used to add a row to a Pandas DataFrame. This function appends a row to a DataFrame and retrieves a new DataFrame object.

Example

Let’s overview the below example code:

import pandas

data1 = pandas.DataFrame({"students": ["Mary", "Queen", "Anna"], "marks": [52, 63, 84], "id_no": [5, 6, 2]})

print('Original DataFrame:\n',data1)

new_row = {'students': 'Emily', 'marks': 30, 'id_no': 8}

data1 = data1.append(new_row, ignore_index=True)

print('\nAfter Adding Row to DataFrame:\n',data1)

In this code:

  • The “pandas” library is imported, and “DataFrame” with three columns is created.
  • The dictionary “new_row” is initialized in the program. This dictionary represents the values of the new row that needs to be added.
  • The “append()” function is used to append the dictionary “new_row” to the target DataFrame.
  • The “ignore_index=True” parameter ensures that the resulting DataFrame has a new index.

Output

Conclusion

To add/insert a row to Pandas DataFrame, the “dataframe.loc[ ]” method, “pandas.concat()” function or the “dataframe.append()” function is used in Python. The “df.loc()” method is used along with the “len()” function to add a complete row to the specified DataFrame. Similarly, the “pandas.concat()” and “append()” functions can also be used to add the new row to the specified DataFrame. This Python post presented a detailed guide on how to add/insert a row to Pandas DataFrame using numerous examples.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.