pandas
  1. pandas-dataframeappend

DataFrame.append() - Pandas DataFrame Basics

The append() method in pandas is used to concatenate or combine two or more DataFrames. It is a helpful method to combine multiple DataFrames into a single one.

Syntax

The basic syntax to append a DataFrame in pandas is as follows:

import pandas as pd

df_new = df1.append(df2)

Here, we create a new DataFrame named df_new by appending df2 to df1.

Example

Consider the following example, where two DataFrames are concatenated using the append() method:

import pandas as pd

df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df2 = pd.DataFrame({'A': [7, 8, 9], 'B': [10, 11, 12]})

df_new = df1.append(df2)

print(df_new)

In this example, we create two DataFrames named df1 and df2, each with two columns 'A' and 'B' and three rows of data. We then concatenate these two DataFrames using the append() method and store the result in the df_new DataFrame.

Output

When the above program is executed, it outputs the following DataFrame:

   A   B
0  1   4
1  2   5
2  3   6
0  7  10
1  8  11
2  9  12

Here, we can see that the concatenated DataFrame df_new contains all the rows from df1 followed by all the rows from df2.

Explanation

The append() method in pandas is used to concatenate two or more DataFrames along a particular axis. By default, it combines DataFrames row-wise i.e, vertically one after another. The axis parameter can be used to concatenate column-wise i.e, horizontally side by side.

Use

The append() method in pandas is used to combine multiple DataFrames into a single one. It is helpful when working with large datasets and merging similar data.

Important Points

  • append() method is used to concatenate two or more DataFrames along a particular axis
  • By default, it combines DataFrames row-wise i.e, vertically one after another.
  • The axis parameter can be used to concatenate column-wise i.e, horizontally side by side.

Summary

The append() method in pandas allows two or more DataFrames to be concatenated into a single DataFrame. By default, it combines DataFrames row-wise and a new DataFrame is returned. It is an essential method when working with larger data sets and merging similar data.

Published on: