 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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
Python Pandas and Numpy - Concatenate multiindex into single index
To concatenate multiindex into single index, at first, let us import the required Pandas and Numpy libraries with their respective aliases −
import pandas as pd import numpy as np
Create Pandas series −
d = pd.Series([('Jacob', 'North'),('Ami', 'East'),('Ami', 'West'),('Scarlett', 'South'),('Jacob', 'West'),('Scarlett', 'North')]) Now, use the Numpy arrange() method −
dataFrame = pd.Series(np.arange(1, 7), index=d)
Let us now map and join −
dataMap = dataFrame.index.map('_'.join) Example
Following is the code −
import pandas as pd import numpy as np # pandas series d = pd.Series([('Jacob', 'North'),('Ami', 'East'),('Ami', 'West'),('Scarlett', 'South'),('Jacob', 'West'),('Scarlett', 'North')]) dataFrame = pd.Series(np.arange(1, 7), index=d) # mapping and joining dataMap = dataFrame.index.map('_'.join) print"\nResult after mapping:\n",dataMap  Output
This will produce the following output −
Result after mapping: Index([u'Jacob_North', u'Ami_East', u'Ami_West', u'Scarlett_South', u'Jacob_West', u'Scarlett_North'],dtype='object')
Advertisements
 