search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas DataFrame | append method

schedule Aug 12, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Pandas DataFrame.append(~) method appends new rows to the source DataFrame. The new rows that are to be added can be in the form of a DataFrame, Series or arrays.

Note that a new DataFrame is returned and the source DataFrame is kept intact.

Parameters

1. otherlink | DataFrame or named Series or dict-like or list of these

The data to append to the source DataFrame.

2. ignore_indexlink | boolean | optional

  • If True, then the resulting index labels would be the default integer indices.

  • If False, then then the resulting index labels of other will be used.

By default, ignore_index=False.

3. verify_integritylink | boolean | optional

If True, then duplicate index names would raise an Error. By default, verify_integrity=False.

4. sortlink | boolean | optional

Whether or not to sort the columns in the resulting DataFrame by column label. By default, sort=False.

WARNING

For performance benefits, instead of repetitively calling append(~), consider appending all the rows at once.

Return Value

A new DataFrame with new rows appended.

Examples

Case when column labels match up

Consider the following DataFrames:

df = pd.DataFrame({"A":[3,4], "B":[5,6]})
df_other = pd.DataFrame({"A":[7,8], "B":[9,10]}, index=["a","b"])
[df] [df_other]
A B A B
0 3 5 a 7 9
1 4 6 b 8 10

Notice how df and df_other both share the same column labels. If such is the case, then append(~) will result in a vertical stacking like so:

df.append(df_other)
   A  B
0  3  5
1  4  6
a  7  9
b  8  10

Case when column labels do not match up

Consider the case when other does not share the same column labels:

df = pd.DataFrame({"A":[3,4], "B":[5,6]})
df_other = pd.DataFrame({"B":[7,8], "C":[9,10]}, index=["a","b"])
[df] [df_other]
A B B C
0 3 5 a 7 9
1 4 6 b 8 10

Here, both df and df_other have column B, but columns A and C do not match up. Calling append(~) in such cases will result in entries with NaN:

df.append(df_other)
   A    B  C
0  3.0  5  NaN
1  4.0  6  NaN
a  NaN  7  9.0
b  NaN  8  10.0

Appending a list of dictionary

Consider the following DataFrame:

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

To append using a list of dictionary:

df.append([{"A":7,"B":8}, {"A":9,"B":10}])
A B
0 3 5
1 4 6
0 7 8
1 9 10

Pandas guarantees that the rows will always be appended according to the list's ordering.

Specifying ignore_index

Consider the following two DataFrames again:

df = pd.DataFrame({"A":[3,4], "B":[5,6]})
df_other = pd.DataFrame({"A":[7,8], "B":[9,10]}, index=["a","b"])
[df] [df_other]
A B A B
0 3 5 a 7 9
1 4 6 b 8 10

By default, ignore_index=False, which means that the index of other will be kept:

df.append(df_other)
   A  B
0  3  5
1  4  6
a  7  9
b  8  10

Notice how the name of the indexes a and b are preserved.

We can set ignore_index=True to ignore the index of other:

df.append(df_other, ignore_index=True)
   A  B
0  3  5
1  4  6
2  7  9
3  8  10

Notice how instead of the index of the new columns are 2 and 3, rather than their original a and b.

Specifying verify_integrity

Consider the following two DataFrames:

df = pd.DataFrame({"B":[3,4], "C":[5,6]}, index=["a","b"])
df_other = pd.DataFrame({"B":[7,8], "C":[9,10]}, index=["b","c"])
[df] [df_other]
B C B C
a 3 5 b 7 9
b 4 6 c 8 10

Notice how both these DataFrames have index b.

By default, verify_integrity=False, which means that duplicate indexes in the resulting DataFrame are allowed:

df.append(df_other) # verify_integrity=False
B C
a 3 5
b 4 6
b 7 9
c 8 10

In contrast, setting verify_integrity=True will throw an error if duplicate indexes exist:

df.append(df_other, verify_integrity=True)
ValueError: Indexes have overlapping values: Index(['b'], dtype='object')

Sorting by column label

By default, sort=False, which means that the resulting DataFrame's columns will not be sorted by column labels:

df = pd.DataFrame({"C":[3,4], "A":[5,6]})
df.append([{"B":7,"E":8}], sort=False)
C A B E
0 3.0 5.0 NaN NaN
1 4.0 6.0 NaN NaN
0 NaN NaN 7.0 8.0

By setting sort=True, the resulting DataFrame's columns can be sorted by column labels:

df = pd.DataFrame({"C":[3,4], "A":[5,6]})
df.append([{"B":7,"E":8}], sort=True)
A B C E
0 5.0 NaN 3.0 NaN
1 6.0 NaN 4.0 NaN
0 NaN 7.0 NaN 8.0
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...