Concatenate multiIndex into single index in Pandas Series - GeeksforGeeks

14 Sep, 2021
In this article, we will see how to concatenate multi-index to a single index in Pandas Series. Multi-index refers to having more than one index with the same name.
Create a sample series:
Python3
# importing pandas moduleimport pandas as pdimport numpy as np # Creating series data for address detailsindex_values = pd.Series([('sravan', 'address1'),                          ('sravan', 'address2'),                          ('sudheer', 'address1'),                          ('sudheer', 'address2')]) # assigning values with integersdata = pd.Series(np.arange(1, 5),                 index=index_values) # display dataprint(data)
Output:
Joining two or more data is known as concatenation. Here we are going to concatenate the index using map function.
Syntax:
map(fun, iter)
fun: function
iter: iterations.
Below are various examples that depict how to concatenate multi-index into a single index in Series:
Example 1:
This code explains the joining of addresses into one based on multi-index.
Python3
# importing pandas moduleimport pandas as pd # Creating series data for address detailsindex_values = pd.Series([('sravan', 'address1'),                          ('sravan', 'address2'),                          ('sudheer', 'address1'),                          ('sudheer', 'address2')]) # assigning values with integersdata = pd.Series(np.arange(1, 5), index=index_values) # display dataprint(data) # mapping with data using '_' symbol with joindata1 = data.index.map('_'.join) print(data1)
Output:
Example 2:
This code is an example for all the given same name, but different values passed in a tuple.
Python3
# importing pandas moduleimport pandas as pd # importing numpy moduleimport numpy as np # Creating series data for address details with same name.index_values = pd.Series([('sravan', 'address1'),                          ('sravan', 'address2'),                          ('sravan', 'address3'),                          ('sravan', 'address4')]) # assigning values with integersdata = pd.Series(np.arange(1, 5),                 index=index_values) # display dataprint(data) # mapping with data using '_' symbol with joindata1 = data.index.map('_'.join) print(data1)
Output:
Example 3: 
This code gives a demo on multiple users given in nested list data structure.
Python3
# importing pandas moduleimport pandas as pd # importing numpy moduleimport numpy as np # Creating series data for address details# with same name with nested lists.index_values = pd.Series([['sravan', 'address1'],                          ['sravan', 'address2'],                          ['sravan', 'address3'],                          ['sravan', 'address4'],                          ['vani', 'address5'],                          ['vani', 'address6'],                          ['vani', 'address7'],                          ['vani', 'address8']]) # assigning values with integersdata = pd.Series(np.arange(1, 9),                 index=index_values) # display dataprint(data) # mapping with data using '_' symbol with joindata1 = data.index.map('_'.join) print(data1)
Output:
Example 4:
This code explains the college data with respect to address passed in a nested list separated by ‘/’ operator.
Python3
# importing pandas moduleimport pandas as pd # importing numpy moduleimport numpy as np # Creating series data for address details w.r.t# college names  with same name with nested lists.index_values = pd.Series([['sravan', 'address1', 'vignan'],                          ['sravan', 'address2', 'vignan'],                          ['sravan', 'address3', 'vignan'],                          ['sravan', 'address4', 'vignan'],                          ['vani', 'address5', 'vignan lara'],                          ['vani', 'address6', 'vignan lara'],                          ['vani', 'address7', 'vignan lara'],                          ['vani', 'address8', 'vignan lara']]) # assigning values with integersdata = pd.Series(np.arange(1, 9),                 index=index_values) # display dataprint(data) # mapping with data using '/' symbol with joindata1 = data.index.map('/'.join) print(data1)
Output:
sagartomar9927
Picked
Python Pandas-exercise
Python pandas-series
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Enumerate() in Python
How to Install PIP on Windows ?
Different ways to create Pandas Dataframe
Python String | replace()
Create a Pandas DataFrame from Lists
Reading and Writing to text files in Python
sum() function in Python
*args and **kwargs in Python
How To Convert Python Dictionary To JSON?