Append rows or DataFrames to a pandas DataFrame

dataframe_append_rows

Skill - Append rows or DataFrames to a pandas DataFrame

Table of Contents

Skills Required

Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision


Pandas is a python library.
DataFrame is a data structure provided by the pandas library.

Please go through Pandas DataFrame Basics to learn the basics of pandas DataFrame.

In this post, we will learn how to append rows or DataFrames to a pandas DataFrame using the append function


Using the ‘append’ function

# import pandas module
import pandas as pd

# create a dataframe
df1 = pd.DataFrame([[1,2],[3,4]], columns=['A', 'B'])
print(df1)
"""
this prints
   A  B
0  1  2
1  3  4
"""
# create another dataframe
df2 = pd.DataFrame([[5,6],[7,8]], columns=['A', 'B'])
print(df2)
"""
this prints
    A  B
0  5  6
1  7  8
"""

# append df2 to df1 using 'append' function
df3 = df1.append(df2)
print(df3)
"""
this prints
   A  B
0  1  2
1  3  4
0  5  6
1  7  8
"""

# append df2 to df1 and create a fresh index
df3 = df1.append(df2, ignore_index=True)
print(df3)
"""
this prints
   A  B
0  1  2
1  3  4
2  5  6
3  7  8
notice the index is reset due to ignore_index option
"""

Using ‘loc’ to append a single row (if confident about the index)

Another way to append row to a DataFrame is to use loc function.
But use this method if are aware of the existing index of the DataFrame, otherwise we might loose the existing data

# import pandas module
import pandas as pd

# create a DataFrame
df = pd.DataFrame([[1,2], [3,4]], columns=['A', 'B'])
print(df)
"""
this prints
   A  B
0  1  2
1  3  4
"""

# add a new row using loc
df.loc[2] = [5,6]
print(df)
"""
this prints
   A  B
0  1  2
1  3  4
2  5  6
"""

# instead of list we can append row using a pandas Series
df.loc[3] = pd.Series({"A":7, "B":9})

# we need not specify all column values while using pandas Series
# NaN will be used for unspecfied columns
df.loc[4] = pd.Series({"A":10})

print(df)
"""
this prints
   A  B
0  1  2
1  3  4
2  5  6
3  7  9
4  10  NaN
"""

Video

Video for this post can be found here


Please read this official documentation for getting know about more options and examples

Online Interpreter

Although we recommend to practice the above examples in Visual Studio Code, you can run these examples online at https://www.tutorialspoint.com/execute_python_online.php

References


Table of Contents

Comments