
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to append two DataFrames in Pandas?
To append the rows of one dataframe with the rows of another, we can use the Pandas append() function. With the help of append(), we can append columns too. Let's take an example and see how to use this method.
Steps
- Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df1.
- Print the input DataFrame, df1.
- Create another DataFrame, df2, with the same column names and print it.
- Use the append method, df1.append(df2, ignore_index=True), to append the rows of df2 with df2.
- Print the resultatnt DataFrame.
Example
import pandas as pd df1 = pd.DataFrame({"x": [5, 2], "y": [4, 7], "z": [9, 3]}) df2 = pd.DataFrame({"x": [1, 3], "y": [1, 9], "z": [29, 30]}) print "Input DataFrame 1 is:\n", df1 print "Input DataFrame 2 is:\n", df2 df3 = df1.append(df2, ignore_index=True) print "After appending, DataFrame is: \n", df3
Output
Input DataFrame 1 is: x y z 0 5 4 9 1 2 7 3 Input DataFrame 2 is: x y z 0 1 1 29 1 3 9 30 After appending, DataFrame is: x y z 0 5 4 9 1 2 7 3 2 1 1 29 3 3 9 30
Now, let's use different column names for the dataframes and use the append() function without ignore_index parameter. The default value of ignore_index is False.
import pandas as pd df1 = pd.DataFrame({"x": [5, 2], "y": [4, 7], "z": [9, 3]}) df2 = pd.DataFrame({"a": [1, 3], "b": [1, 9], "c": [29, 30]}) print "Input DataFrame 1 is:\n", df1 print "Input DataFrame 2 is:\n", df2 df3 = df1.append(df2) print "After appending, DataFrame is: \n", df3
Now, it will produce the following output
Input DataFrame 1 is: x y z 0 5 4 9 1 2 7 3 Input DataFrame 2 is: a b c 0 1 1 29 1 3 9 30 After appending, DataFrame is: x y z a b c 0 5.0 4.0 9.0 NaN NaN NaN 1 2.0 7.0 3.0 NaN NaN NaN 0 NaN NaN NaN 1.0 1.0 29.0 1 NaN NaN NaN 3.0 9.0 30.0
- Related Questions & Answers
- Python - How to Concatenate more than two Pandas DataFrames?
- How to compare two DataFrames in Python Pandas with missing values
- Python - How to Concatenate Two or More Pandas DataFrames along rows?
- Python - How to Concatenate Two or More Pandas DataFrames along columns?\n
- Python Pandas – Find the Difference between two Dataframes
- Python Pandas - Finding the uncommon rows between two DataFrames
- Python - Fetch columns between two Pandas DataFrames by Intersection
- Python Pandas – Check if two Dataframes are exactly same
- How to append a list to a Pandas DataFrame using append() in Python?
- How to plot histograms from dataframes in Pandas using Matplotlib?
- How to append elements to a Pandas series?
- Plotting multiple dataframes using Pandas functionality
- Python - Concatenate Pandas DataFrames Without Duplicates
- Python Pandas – Fetch the Common rows between two DataFrames with concat()
- Python Pandas – Find the common rows between two DataFrames with merge()
Advertisements