Python | Pandas Index.append()
Last Updated :
24 Aug, 2023
Python is an excellent language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas are one of those packages, making importing and analyzing data much easier.
Pandas
Index.append()
The function is used to append a single or a collection of indices together. In the case of a collection of indices, all of them get appended to the original index in the same order as they are passed to the Index.append()
function. The function returns an appended index.
Syntax:
Index.append(other)
Parameters :
other : Index or list/tuple of indices
Returns : Index
Example 1: Use Index.append()
function to append a single index to the given index.
Python3
import pandas as pd
df1 = pd.Index([ 17 , 69 , 33 , 5 , 0 , 74 , 0 ])
df2 = pd.Index([ 11 , 16 , 54 , 58 ])
print (df1, "\n" , df2)
|
Output :
Int64Index([17, 69, 33, 5, 0, 74, 0], dtype='int64')
Int64Index([11, 16, 54, 58], dtype='int64')
Let’s append the df2 index at the end of df1.
Output :
Int64Index([17, 69, 33, 5, 0, 74, 0, 11, 16, 54, 58], dtype='int64')
As we can see in the output, the second index i.e. df2 has been appended at the end of df1 .
Example 2: Use Index.append()
function to append a collection of indexes at the end of the given index.
Python3
import pandas as pd
df1 = pd.Index([ 'Jan' , 'Feb' , 'Mar' , 'Apr' ])
df2 = pd.Index([ 'May' , 'Jun' , 'Jul' , 'Aug' ])
df3 = pd.Index([ 'Sep' , 'Oct' , 'Nov' , 'Dec' ])
print (df1, "\n" , df2, "\n" , df3)
|
Output :
Index(['Jan', 'Feb', 'Mar', 'Apr'], dtype='object')
Index(['May', 'Jun', 'Jul', 'Aug'], dtype='object')
Index(['Sep', 'Oct', 'Nov', 'Dec'], dtype='object')
Let’s append both the indexes df2 and df3 at the end of df1.
Output :
Index(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct',
'Nov', 'Dec'],
dtype='object')
Share your thoughts in the comments
Please Login to comment...