Pandas dataframe.append()
×


Pandas dataframe.append()

668

Pandas DataFrame.append() Method

The DataFrame.append() method in pandas is used to append rows of one DataFrame to the end of another DataFrame, returning a new DataFrame object. This method is particularly useful when you need to combine multiple DataFrames or add new rows to an existing DataFrame.

Syntax

DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=False)

Parameters:

  • other: DataFrame, Series, dict-like, or list of these. The data to append.
  • ignore_index: bool, default False. If True, do not use the index labels. The resulting DataFrame will have a new integer index.
  • verify_integrity: bool, default False. If True, checks whether the new concatenated axis contains duplicates. Raises ValueError if duplicates are found.
  • sort: bool, default False. If True, sorts the columns to ensure the resulting DataFrame has the same column order.

Example 1: Appending One DataFrame to Another

Consider two DataFrames:

import pandas as pd

# Creating the first DataFrame
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})

# Creating the second DataFrame
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})

# Appending df2 to df1
df_appended = df1.append(df2, ignore_index=True)

print(df_appended)

Output:

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

In this example, df2 is appended to df1, and the ignore_index=True parameter ensures that the index is reset in the resulting DataFrame.

Example 2: Appending a Dictionary as a Row

You can also append a single row represented as a dictionary:

new_row = {'A': 7, 'B': 9}
df_appended = df1.append(new_row, ignore_index=True)

print(df_appended)

Output:

   A  B
0  1  3
1  2  4
2  7  9

Here, a new row with values 7 and 9 is appended to df1.

Example 3: Appending a List as a Row

If you have a list and want to append it as a new row:

new_data = [8, 10]
df_appended = df1.append(pd.Series(new_data, index=df1.columns), ignore_index=True)

print(df_appended)

Output:

   A   B
0  1   3
1  2   4
2  8  10

In this case, the list new_data is converted to a Series and appended as a new row.

Important Notes

  • The append() method does not modify the original DataFrame; it returns a new DataFrame with the appended rows.
  • As of pandas version 2.0, the append() method has been removed. It is recommended to use pd.concat() for appending rows to a DataFrame.

Conclusion

The DataFrame.append() method is a convenient way to add rows to a DataFrame. However, with its removal in pandas 2.0, transitioning to pd.concat() is advisable for future-proofing your code. Understanding these methods enhances your ability to manipulate and analyze data efficiently in pandas.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat