Pandas DataFrame append() Method

Rate this post

Preparation

Before any data manipulation can occur, two (2) new libraries will require installation.

  • The Pandas library enables access to/from a DataFrame.
  • The NumPy library supports multi-dimensional arrays and matrices in addition to a collection of mathematical functions.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install numpy

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.


FeFeel free to view the PyCharm installation guide for the required libraries.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import numpy

DataFrame append()

The append() method adds rows to the bottom (end) of a DataFrame/Series. A new DataFrame/Series returns with the appropriate rows appended. Columns not existing in the calling object add as new column(s).

The syntax for this method is as follows:

DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=False)
ParameterDescription
otherThis parameter can be a DataFrame, Series, dictionary, or a list. These column(s) append to the original calling object.
ignore_indexIf True, ignore the original index: False use the original index.
verify_integrityIf True, raise a ValueError if duplicates exist.
sortSort the column(s) if the calling object and the other parameter do not align.

For this example, we have two (2) DataFrames. One with existing customer login credentials and one with new customer credentials. The code below appends them to form one (1) DataFrame.

Code – Example 1

df_custs = pd.DataFrame({('jkende',  'Vzs*@4:kNq%)'), 
                         ('sarahJ',  '{M$*3zB~-a-W'), 
                         ('AmyKerr', '*7#<bSt?Y_Z<')}, 
                         columns=['username', 'password'], 
                         index=['user-a', 'user-b', 'user-c'])
print(df_custs)

df_new = pd.DataFrame({('twilles',    '&4&F#@[>g$+%'), 
                         ('cindylou',   'JBW!ktA3;9sD')},
                         columns=['username', 'password'], 
                         index=['user-d', 'user-e'])
print(df_new)

df = df_custs.append(df_new)
print(df)
  • Line [1] creates a DataFrame from a dictionary of tuples and assigns it to df_custs.
  • Line [2] outputs this DataFrame to the terminal.
  • Line [3] creates a DataFrame from a dictionary of tuples and assigns it to df_new.
  • Line [4] outputs this DataFrame to the terminal.
  • Line [5] appends the DataFrame df_new to the end of the DataFrame df_custs. This output saves to a new DataFrame (df).
  • Line [6] outputs this DataFrame to the terminal.

Output

df_custs

 usernamepassword
user-a  jkende Vzs*@4:kNq%)
user-b AmyKerr *7#<bSt?Y_Z<
user-c  sarahJ {M$*3zB~-a-W

df_new

 usernamepassword
user-dtwilles &4&F#@[>g$+%
user-ecindylou JBW!ktA3;9sD

df

 usernamepassword
user-a  jkende Vzs*@4:kNq%)
user-b AmyKerr *7#<bSt?Y_Z<
user-c  sarahJ {M$*3zB~-a-W
user-dtwilles &4&F#@[>g$+%
user-ecindylou JBW!ktA3;9sD

For this example, one (1) record is appended to the DataFrame df_custs using loc.

Code – Example 2

df_custs = pd.DataFrame({('jkende',  'Vzs*@4:kNq%)'), 
                         ('sarahJ',  '{M$*3zB~-a-W'), 
                         ('AmyKerr', '*7#<bSt?Y_Z<')}, 
                         columns=['username', 'password'], 
                         index=['user-a', 'user-b', 'user-c'])

df_custs.loc['user-d'] = ('jkende',  'Vzs*@4:kNq%)')
print(df_custs)
  • Line [1] creates a DataFrame from a Dictionary of Tuples and assigns it to df_custs.
  • Line [2] uses loc to append one (1) record to the end of the DataFrame.
  • Line [3] outputs the DataFrame to the terminal.

Output

df_custs

 usernamepassword
user-a  jkende Vzs*@4:kNq%)
user-b AmyKerr *7#<bSt?Y_Z<
user-c  sarahJ {M$*3zB~-a-W

updated df_custs

 usernamepassword
user-a  jkende Vzs*@4:kNq%)
user-b AmyKerr *7#<bSt?Y_Z<
user-c  sarahJ {M$*3zB~-a-W
user-dtwilles &4&F#@[>g$+%

More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.