Hit enter after type your search item

3 Examples to Append DataFrame by Pandas conact()

How to append DataFrame in Pandas

In order to append a DataFrame, Pandas has DataFrame.append method.

However, the append method is deprecated since 1.4.0 version.

Instead of the append method, you should use Pandas.concat method to add/append data in the data frames.

In this tutorial, we will show you examples of using the concat() method for joining two or more data frames in Python programs.

Syntax of concat()

Following is the complete syntax of concat() method in Pandas:

pandas.concat(objs, *, axis=0, join=’outer’, ignore_index=False, keys=None, levels=None, names=None, verify_integrity=False, sort=False, copy=True)

Let us use concat() method with a few of its parameters below.

An example of concatenating two data frames

In this example, we have created a data frame based on the Python list.

This data frame is displayed on the screen.

This is followed by creating another Data Frame based on list 2. This data frame is also displayed on the screen.

Then we appended that to the first DF by concat() method and displayed concatenated DFs.

Python program:


Output:

pandas-concat-dataframe

An example of using concat() method with Dict

In the above example, you have seen the columns are added ahead after we concatenated the first DF to the second DF.

In the example below, we will create two dictionaries in Python.

The first dictionary contains Column names and three records of the telephone directory (Name and Phone No). On that bases, we created a DataFrame object.

This data frame is displayed on the screen.

This is followed by creating another dictionary which is used for creating a second Data Frame. There we created a row to be appended at the end of the first DF.

For clarity, data frames before and after concatenation are displayed:

Python program:


Result:

Notice the axis parameter in the concat method. This time we used axis=0.

As a result, the second data frame is appended at the end of the first DF, and though the second DF also contained column names, the concatenated result does not include this.

So, if we have used axis = 1, as in the first example the result:

concat-dataframe-axis=

An example of concatenating two data frames and using keys parameter

You may also use the keys parameter of the concat() method for constructing the hierarchical index.

In that case, the passed keys are used at the outermost level.

See the example below where we created three data frames and concatenated along with keys:


Output:

concat-DFs-keys

This div height required for enabling the sticky sidebar