search
Search
Publish
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
chevron_left Row and Column Operations Cookbook
Adding a column that contains the difference in consecutive rowsAdding a constant number to DataFrame columnsAdding an empty column to a DataFrameAdding column to DataFrame with constant valuesAdding new columns to a DataFrameAppending rows to a DataFrameApplying a function that takes as input multiple column valuesApplying a function to a single column of a DataFrameChanging column type to categoricalChanging the name of a DataFrame's indexChanging the order of columns in a DataFrameChanging the type of a DataFrame's indexChanging the type of a DataFrame's columnChecking if a column exists in a DataFrameChecking if a DataFrame column contains some valuesChecking if a value exists in a DataFrame in PandasChecking if column is numericChecking the data type of columnsChecking whether column values match or contain a patternCombining two columns as a single column of tuplesCombining two columns of type string in a DataFrameComputing the average of columnsComputing the correlation between columnsConcatenating DataFrames horizontallyConcatenating DataFrames verticallyConverting a row to column labelsConverting categorical type to intConverting column to listConverting Index to listConverting percent strings into numericConverting the index of a DataFrame into a columnCounting duplicate rowsCounting number of rows with no missing valuesCounting the occurrence of values in columnsCounting unique values in a column of a DataFrameCounting unique values in rows of a DataFrameCreating a new column based on other columnsCreating new column using if, elif and elseDescribing certain columnsDropping columns whose label contains a substringGetting column values based on another column values in a DataFrame in PandasGetting columns as a copyGetting columns whose label contains a substringGetting maximum value in columnsGetting maximum value of entire DataFrameGetting mean of columnsGetting median of columnsGetting minimum value in columnsGetting row label when calling applyGetting row labels as listGetting rows where column value contains any substring in a listGetting the name of indexGetting type of indexGrouping DataFrame rows into listsInserting column at a specific locationIterating over each column of a DataFrameIterating over each row of a DataFrameModifying rows of a DataFrameModifying values in IndexRemoving columns from a DataFrameRemoving columns using column labelsRemoving columns using integer indexRemoving columns with all missing valuesRemoving columns with some missing valuesRemoving duplicate columnsRemoving duplicate rowsRemoving first n rows of a DataFrameRemoving multiple columnsRemoving prefix from column labelsRemoving rows at random without shufflingRemoving rows from a DataFrame based on column valuesRemoving rows using integer indexRemoving rows with all zerosRemoving suffix from column labelsRenaming columns of a DataFrameReplacing substring in column valuesReturning multiple columns using the apply functionReversing the order of rowsSetting a new index of a DataFrameSetting an existing column as the new indexSetting column as the indexSetting integers as column labelsShowing all column labelsShuffling the rows of a DataFrameSorting a DataFrame by columnSorting a DataFrame by indexSorting DataFrame alphabeticallySorting DataFrame by column labelsSplitting a column of strings into multiple columnsSplitting column of lists into multiple columnsSplitting dictionary into separate columnsStripping substrings from values in columnsStripping whitespace from columnsStripping whitespaces in column labelsSumming a column of a DataFrameSumming rows of specific columnsSwapping the rows and columns of a DataFrameUpdating a row while iterating over the rows of a DataFrameUpdating rows based on column valuesUsing apply method in parallel
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
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
A
A
share
thumb_up_alt
bookmark
arrow_backShare
Twitter
Facebook
chevron_left Row and Column Operations Cookbook
Adding a column that contains the difference in consecutive rowsAdding a constant number to DataFrame columnsAdding an empty column to a DataFrameAdding column to DataFrame with constant valuesAdding new columns to a DataFrameAppending rows to a DataFrameApplying a function that takes as input multiple column valuesApplying a function to a single column of a DataFrameChanging column type to categoricalChanging the name of a DataFrame's indexChanging the order of columns in a DataFrameChanging the type of a DataFrame's indexChanging the type of a DataFrame's columnChecking if a column exists in a DataFrameChecking if a DataFrame column contains some valuesChecking if a value exists in a DataFrame in PandasChecking if column is numericChecking the data type of columnsChecking whether column values match or contain a patternCombining two columns as a single column of tuplesCombining two columns of type string in a DataFrameComputing the average of columnsComputing the correlation between columnsConcatenating DataFrames horizontallyConcatenating DataFrames verticallyConverting a row to column labelsConverting categorical type to intConverting column to listConverting Index to listConverting percent strings into numericConverting the index of a DataFrame into a columnCounting duplicate rowsCounting number of rows with no missing valuesCounting the occurrence of values in columnsCounting unique values in a column of a DataFrameCounting unique values in rows of a DataFrameCreating a new column based on other columnsCreating new column using if, elif and elseDescribing certain columnsDropping columns whose label contains a substringGetting column values based on another column values in a DataFrame in PandasGetting columns as a copyGetting columns whose label contains a substringGetting maximum value in columnsGetting maximum value of entire DataFrameGetting mean of columnsGetting median of columnsGetting minimum value in columnsGetting row label when calling applyGetting row labels as listGetting rows where column value contains any substring in a listGetting the name of indexGetting type of indexGrouping DataFrame rows into listsInserting column at a specific locationIterating over each column of a DataFrameIterating over each row of a DataFrameModifying rows of a DataFrameModifying values in IndexRemoving columns from a DataFrameRemoving columns using column labelsRemoving columns using integer indexRemoving columns with all missing valuesRemoving columns with some missing valuesRemoving duplicate columnsRemoving duplicate rowsRemoving first n rows of a DataFrameRemoving multiple columnsRemoving prefix from column labelsRemoving rows at random without shufflingRemoving rows from a DataFrame based on column valuesRemoving rows using integer indexRemoving rows with all zerosRemoving suffix from column labelsRenaming columns of a DataFrameReplacing substring in column valuesReturning multiple columns using the apply functionReversing the order of rowsSetting a new index of a DataFrameSetting an existing column as the new indexSetting column as the indexSetting integers as column labelsShowing all column labelsShuffling the rows of a DataFrameSorting a DataFrame by columnSorting a DataFrame by indexSorting DataFrame alphabeticallySorting DataFrame by column labelsSplitting a column of strings into multiple columnsSplitting column of lists into multiple columnsSplitting dictionary into separate columnsStripping substrings from values in columnsStripping whitespace from columnsStripping whitespaces in column labelsSumming a column of a DataFrameSumming rows of specific columnsSwapping the rows and columns of a DataFrameUpdating a row while iterating over the rows of a DataFrameUpdating rows based on column valuesUsing apply method in parallel
thumb_up
0
thumb_down
0
chat_bubble_outline
0
auto_stories new
settings

Appending rows to a Pandas DataFrame

Programming
chevron_right
Python
chevron_right
Pandas
chevron_right
Cookbooks
chevron_right
DataFrame Cookbooks
chevron_right
Row and Column Operations Cookbook
schedule Mar 10, 2022
Last updated
local_offer PythonPandas
Tags

To append a row (a list or Series) to a Pandas DataFrame, use the DataFrame's append(~) method.

WARNING

For performance, instead of appending rows one by one, opt to append rows at once.

Appending a single row

Consider the following DataFrame:

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

From a list

To use append(~), we first need to convert our row, which is initially represented as a list, to a Series:

my_row = [10,11]
s = pd.Series(my_row, index=df.columns)
df = df.append(s, ignore_index=True)
df
A B
0 3 5
1 4 6
2 10 11

Here, note the following:

  • the index of the Series s has been set to the column labels of df (A and B) - otherwise, the append(~) method won't vertically stack them.

  • we set ignore_index=True to ignore the index of s - otherwise, we would have duplicate values in the new index 0 1 0.

  • a new DataFrame is returned, and the original df is kept is intact.

From a Series

Consider the same DataFrame:

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

Suppose we wanted to append the following Series to df:

s = pd.Series([10,11], index=df.columns)
s
A 10
B 11
dtype: int64

Here, notice how the index of the Series has been set as the column labels - otherwise, the append(~) method won't vertically stack them.

Now, call append(~) like so:

df.append(s, ignore_index=True)
A B
0 3 5
1 4 6
2 10 11

The ignore_index=True is required because we did not assign a name to our Series s, and so Pandas does not know what row label to use when it is added to the DataFrame. Passing ignore_index tells Pandas to use the default integer index, which in this case is 2.

Appending multiple rows

Consider the same df as above:

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

From a nested list

To append multiple rows that are represented as a nested list:

my_list = [[10,11],[12,13]]
df.append(pd.DataFrame(my_list, columns=df.columns), ignore_index=True)
A B
0 3 5
1 4 6
2 10 11
3 12 13

Here, we are first creating an intermediate DataFrame out of my_list:

pd.DataFrame(my_list, columns=df.columns)
A B
0 10 11
1 12 13

We then call append(~) to vertically stack the DataFrames. The ignore_index=True tells Pandas to use the default integer index in the resulting DataFrame - otherwise we would have gotten duplicate index [0,1,0,1].

From a list of Series

To append multiple rows represented as a list of Series:

my_series = [pd.Series([10,11], index=df.columns), pd.Series([12,13], index=df.columns)]
df.append(my_series, ignore_index=True)
A B
0 3 5
1 4 6
2 10 11
3 12 13

Notice how, in this case, we do not need to create an intermediate DataFrame. This is because append(~) can handle a list of Series by default.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Ask a question or leave a feedback...