Pandas DataFrame DataFrame.append() function
pandas.DataFrame.append() takes a DataFrame as input and merges its rows with the rows of the calling DataFrame, returning a new DataFrame. If any columns in the input DataFrame do not exist in the calling DataFrame, then those columns will be added to the DataFrame and the missing values will be set to NaN
.
pandas.DataFrame.append()
Method Syntax
DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=False)
parameter
other |
Input DataFrame or Series, or Python Dictionary-like, whose rows will be appended |
ignore_index |
Boolean. If yes True , ignore the index in the original DataFrame. The default value is yes , which means use the index False .False |
verify_integrity |
Boolean. If true True , raises when creating an index with duplicates ValueError . The default value isFalse |
sort |
Boolean. If the columns are not aligned, it will sort the original data and the other DataFrame |
Example Code: pandas.DataFrame.append()
Add two DataFrames using
import pandas as pd
names_1=['Hisila', 'Brian','Zeppy']
salary_1=[23,30,21]
names_2=['Ram','Shyam',"Hari"]
salary_2=[22,23,31]
df_1 = pd.DataFrame({'Name': names_1, 'Salary': salary_1})
df_2 = pd.DataFrame({'Name': names_2, 'Salary': salary_2})
merged_df = df_1.append(df_2)
print(merged_df)
Output:
Name Salary
0 Hisila 23
1 Brian 30
2 Zeppy 21
Name Salary
0 Ram 22
1 Shyam 23
2 Hari 31
Name Salary
0 Hisila 23
1 Brian 30
2 Zeppy 21
0 Ram 22
1 Shyam 23
2 Hari 31
It df_1
adds at the end of df_2
, and returns merged_df
, the rows of the two DataFrames merged. Here, merged_df
the indices of are the same as their parent DataFrames.
Example Code: pandas.DataFrame.append()
Append a DataFrame and Ignore the Index Using
import pandas as pd
names_1=['Hisila', 'Brian','Zeppy']
salary_1=[23,30,21]
names_2=['Ram','Shyam',"Hari"]
salary_2=[22,23,31]
df_1 = pd.DataFrame({'Name': names_1, 'Salary': salary_1})
df_2 = pd.DataFrame({'Name': names_2, 'Salary': salary_2})
merged_df = df_1.append(df_2,ignore_index=True)
print(df_1)
print(df_2)
print( merged_df)
Output:
Name Salary
0 Hisila 23
1 Brian 30
2 Zeppy 21
Name Salary
0 Ram 22
1 Shyam 23
2 Hari 31
Name Salary
0 Hisila 23
1 Brian 30
2 Zeppy 21
3 Ram 22
4 Shyam 23
5 Hari 31
It df_2
appends df_1
to the end of , where a new index is obtained merged_df
by using the parameter append()
in the method .ignore_index=True
DataFrame.append()
Set in methodverify_integrity=True
If we append()
set it in the method verify_integrity=True
, we will get duplicate indexes ValueError
.
import pandas as pd
names_1=['Hisila', 'Brian','Zeppy']
salary_1=[23,30,21]
names_2=['Ram','Shyam',"Hari"]
salary_2=[22,23,31]
df_1 = pd.DataFrame({'Name': names_1, 'Salary': salary_1})
df_2 = pd.DataFrame({'Name': names_2, 'Salary': salary_2})
merged_df = df_1.append(df_2,verify_integrity=True)
print(df_1)
print(df_2)
print( merged_df)
Output:
ValueError: Indexes have overlapping values: Int64Index([0, 1, 2], dtype='int64')
Since the elements df_1
in and df_2
have the same index by default, this results in ValueError
. To prevent this error, we use verify_integrity
the default value of , which is verify_integrity=False
.
Example Code: Adding DataFrame with Different Columns
If we append a different column DataFrame
, this column is added to the generated DataFrame
, and the corresponding cells of the column that do not exist in the original DataFrame
or other are set to .DataFrame
NaN
import pandas as pd
names_1=['Hisila', 'Brian','Zeppy']
salary_1=[23,30,21]
names_2=['Ram','Shyam',"Hari"]
salary_2=[22,23,31]
Age=[30,31,33]
df_1 = pd.DataFrame({'Name': names_1, 'Salary': salary_1})
df_2 = pd.DataFrame({'Name': names_2, 'Salary': salary_2,"Age":Age})
merged_df = df_1.append(df_2, sort=False)
print(df_1)
print(df_2)
print( merged_df)
Output:
Name Salary
0 Hisila 23
1 Brian 30
2 Zeppy 21
Name Salary Age
0 Ram 22 30
1 Shyam 23 31
2 Hari 31 33
Name Salary Age
0 Hisila 23 NaN
1 Brian 30 NaN
2 Zeppy 21 NaN
0 Ram 22 30.0
1 Shyam 23 31.0
2 Hari 31 33.0
Here, df_1
the rows of get the value Age
of the column NaN
because Age
the column only exists in df_2
.
We have also set it up sort=False
to silence warnings about sort being deprecated in a future version of Pandas.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Convert Pandas to CSV without index
Publish Date:2025/05/01 Views:159 Category:Python
-
As you know, an index can be thought of as a reference point used to store and access records in a DataFrame. They are unique for each row and usually range from 0 to the last row of the DataFrame, but we can also have serial numbers, dates
Convert Pandas DataFrame to Dictionary
Publish Date:2025/05/01 Views:197 Category:Python
-
This tutorial will show you how to convert a Pandas DataFrame into a dictionary with the index column elements as keys and the corresponding elements of other columns as values. We will use the following DataFrame in the article. import pan
Convert Pandas DataFrame columns to lists
Publish Date:2025/05/01 Views:191 Category:Python
-
When working with Pandas DataFrames in Python, you often need to convert the columns of the DataFrame into Python lists. This process is very important for various data manipulation and analysis tasks. Fortunately, Pandas provides several m
Subtracting Two Columns in Pandas DataFrame
Publish Date:2025/05/01 Views:120 Category:Python
-
Pandas can handle very large data sets and has a variety of functions and operations that can be applied to the data. One of the simple operations is to subtract two columns and store the result in a new column, which we will discuss in thi
Dropping columns by index in Pandas DataFrame
Publish Date:2025/05/01 Views:99 Category:Python
-
DataFrames can be very large and can contain hundreds of rows and columns. It is necessary to master the basic maintenance operations of DataFrames, such as deleting multiple columns. We can use dataframe.drop() the method to delete columns
Pandas Copy DataFrame
Publish Date:2025/05/01 Views:53 Category:Python
-
This tutorial will show you how to DataFrame.copy() copy a DataFrame object using the copy method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 ], "Cost" : [ "300" , "400" , "350" ], } ) print (items_df) Output:
Pandas DataFrame.ix[] Function
Publish Date:2025/05/01 Views:168 Category:Python
-
Python Pandas DataFrame.ix[] function slices rows or columns based on the value of the argument. pandas.DataFrame.ix[] grammar DataFrame . ix[index = None , label = None ] parameter index Integer or list of integers used to slice row indice
Pandas DataFrame.describe() Function
Publish Date:2025/05/01 Views:120 Category:Python
-
Python Pandas DataFrame.describe() function returns the statistics of a DataFrame. pandas.DataFrame.describe() grammar DataFrame . describe( percentiles = None , include = None , exclude = None , datetime_is_numeric = False ) parameter perc
Pandas DataFrame.astype() Function
Publish Date:2025/05/01 Views:160 Category:Python
-
Python Pandas DataFrame.astype() function changes the data type of an object to the specified data type. pandas.DataFrame.astype() grammar DataFrame . astype(dtype, copy = True , errors = "raise" ) parameter dtype The data type we want to a