How to append two DataFrames in Pandas

You can append two DataFrames in Pandas with any of the following methods. I highly recommend you This book to learn Python. In this article, you will see 2 methods to append two DataFrames in Pandas.

Step 1: Install Pandas Library

Install the Pandas library using this code, if it is not installed.

pip install pandas

Method 1: pd.concat()

# Import the Pandas library as pd
import pandas as pd

# Initialize a dictionary
dict1 = {'Names':['Hussain', 'John', 'Ali', 'Satish'],
        'Marks':[75, 80, 85, 82]}

# Convert the dictionary into DataFrame
df1 = pd.DataFrame(dict1)

# Initialize another dictionary
dict2 = {'Names':['Harry', 'Ponting', 'Richard', 'Messi'],
        'Marks':[97, 95, 92, 98]}

# Convert the dictionary into DataFrame
df2 = pd.DataFrame(dict2)

# Now append these two DataFrames
df = pd.concat([df1, df2], ignore_index=True, sort=False)

# Display the Final Output after append
print(df)

Output:

     Names  Marks
0  Hussain     75
1     John     80
2      Ali     85
3   Satish     82
4    Harry     97
5  Ponting     95
6  Richard     92
7    Messi     98

Method 2: df.append()

# Import the Pandas library as pd
import pandas as pd

# Initialize a dictionary
dict1 = {'Names':['Hussain', 'John', 'Ali', 'Satish'],
        'Marks':[75, 80, 85, 82]}

# Convert the dictionary into DataFrame
df1 = pd.DataFrame(dict1)

# Initialize another dictionary
dict2 = {'Names':['Harry', 'Ponting', 'Richard', 'Messi'],
        'Marks':[97, 95, 92, 98]}

# Convert the dictionary into DataFrame
df2 = pd.DataFrame(dict2)

# # Now append these two DataFrames
df = df1.append(df2, ignore_index=True)

# Display the Final Output after append
print(df)

Output:

     Names  Marks
0  Hussain     75
1     John     80
2      Ali     85
3   Satish     82
4    Harry     97
5  Ponting     95
6  Richard     92
7    Messi     98

Free Learning Resources

Leave a Comment

Your email address will not be published.