Pandas DataFrame | append method
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. other
link | DataFrame
or named Series
or dict-like
or list
of these
The data to append to the source DataFrame.
2. ignore_index
link | boolean
| optional
If
True
, then the resulting index labels would be the default integer indices.If
False
, then then the resulting index labels ofother
will be used.
By default, ignore_index=False
.
3. verify_integrity
link | boolean
| optional
If True
, then duplicate index names would raise an Error. By default, verify_integrity=False
.
4. sort
link | boolean
| optional
Whether or not to sort the columns in the resulting DataFrame by column label. By default, sort=False
.
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:
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 B0 3 51 4 6a 7 9b 8 10
Case when column labels do not match up
Consider the case when other
does not share the same column labels:
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 C0 3.0 5 NaN1 4.0 6 NaNa NaN 7 9.0b NaN 8 10.0
Appending a list of dictionary
Consider the following DataFrame:
df
A B0 3 51 4 6
To append using a list of dictionary:
df.append([{"A":7,"B":8}, {"A":9,"B":10}])
A B0 3 51 4 60 7 81 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:
By default, ignore_index=False
, which means that the index of other
will be kept:
df.append(df_other)
A B0 3 51 4 6a 7 9b 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 B0 3 51 4 62 7 93 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:
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 Ca 3 5b 4 6b 7 9c 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.append([{"B":7,"E":8}], sort=False)
C A B E0 3.0 5.0 NaN NaN1 4.0 6.0 NaN NaN0 NaN NaN 7.0 8.0
By setting sort=True
, the resulting DataFrame's columns can be sorted by column labels:
df.append([{"B":7,"E":8}], sort=True)
A B C E0 5.0 NaN 3.0 NaN1 6.0 NaN 4.0 NaN0 NaN 7.0 NaN 8.0