+

Python | Pandas Index.append ()

Index.append() Pandas Index.append() is used to add one or a set of indexes together ... In the case of a collection of indices, they are all appended to the original index in the same order in which they are passed to Index.append () . The function returns the added index.

Syntax: Index.append (other)

Parameters:
other: Index or list / tuple of indices

Returns: appended: bool or array_like (if axis is specified)
A single element array_like may be converted to bool.

Example # 1: Use Index.append () to add a separate index to the given index.

# import pandas as pd

import pandas as pd

 
# Create first index

df1 = pd.Index ([ 17 , 69 , 33 , 5 , 0 , 74 , 0 ])

  
# Create second index

df2 = pd.Index ([ 11 , 16 , 54 , 58 ])

 
# Print the first and second index

print (df1, "" , df2)

Output:

Let`s add a df2 index at the end of df1.

# add df2 at the end of df1
df1.append (df2)

Output:

As we see in the output, the second index, i.e.  df2 was added at the end of df1 .

Example # 2: Use Index.append () to add the index collection at the end of the given index.

# import pandas as pd

import pandas as pd

 
# Create first index

df1 = pd.Index ([ `Jan` , `Feb` , ` Mar` , `Apr` ])

  
# Create second index

df2 = pd.Index ([ `May` , ` Jun` , ` Jul` , `Aug` ])

 
# Create third index

df3 = pd.Index ([ `Sep` , ` Oct` , `Nov` , ` Dec` ])

 
# Print the first, second and third index

print (df1, "" , df2, "" , df3)

Output:

Let`s add both df2 and df3 indices at the end of df1 .

# We pass df2 and df3 as a list
# pointers to the add function
df1.append ([df2, df3])

Output:

Get Solution for free from DataCamp guru