Pandas: How to prepend a row to a DataFrame (4 approaches)

Updated: February 22, 2024 By: Guest Contributor Post a comment

Overview

Pandas is an incredibly powerful and popular library in Python for data manipulation and analysis. One of the common tasks you might find yourself needing to perform is adding a row to the top of a DataFrame. Whether it’s for data prepending or simply reordering your dataset, this tutorial will guide you through four different methods of how to prepend a row to a DataFrame, advancing from basic techniques to more advanced ones.

Using pd.concat()

Perhaps the simplest and most straightforward way to prepend a row to a DataFrame is by using the pd.concat() function. This function allows you to concatenate objects along a particular axis with optional set logic along the other axes.

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
new_row = pd.DataFrame({'A': [0], 'B': [3]})

result_df = pd.concat([new_row, df], ignore_index=True)
print(result_df)

The output of this code snippet will be:

   A  B
0  0  3
1  1  4
2  2  5
3  3  6

Note: By setting ignore_index=True, we ensure that the resulting DataFrame has a continuous index.

Using append() method invertedly

An alternative method, which is somewhat inverted, is to add the existing DataFrame to the new row using the append() method. This allows for a more natural syntax but achieves the same result.

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
new_row = pd.DataFrame({'A': [0], 'B': [3]})

result_df = new_row.append(df, ignore_index=True)
print(result_df)

The output here is the same as the first method, showcasing the flexibility in approach that Pandas offers.

Using pd.DataFrame.loc

For those who are working with indexes and might want a bit more control over the insertion process, using pd.DataFrame.loc can be a powerful method. This involves shifting the index and appending the new row directly.

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}, index=[1, 2, 3])
new_row = pd.Series({'A': 0, 'B': 3}, name=0)

df = df.append(new_row)
df = df.sort_index().reset_index(drop=True)
print(df)

This method not only allows you to prepend a row but also to insert a row anywhere in the DataFrame by adjusting the name property of the Series object (representing the new row) and sorting the index accordingly.

Modifying the DataFrame in-place using iloc

Last but not least, for those looking for in-place modifications without creating a new DataFrame, the iloc functionality offers a more advanced but very flexible option. This method is particularly useful for large DataFrames where performance might be a consideration.

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
new_row = pd.DataFrame({'A': [0], 'B': [3]}, index=[-1])

df = pd.concat([new_row, df]).reset_index(drop=True)
print(df)

Here, we utilized pd.concat() in a slightly different manner by assigning a negative index to the new row. This ensures that when we reset the index, the new row remains at the top. This method is efficient and succinct, offering a balance between readability and performance.

Conclusion

This tutorial walked you through four different methods of prepending a row to a DataFrame in Pandas, ranging from beginner to more advanced techniques. Regardless of the method you choose, it’s essential to understand the implications of each approach, especially concerning index handling and performance. By mastering these techniques, you’ll be well-equipped to handle various data manipulation tasks with ease and efficiency.