Mastering MultiIndex in Pandas for Higher Dimensional Data

🚀 Day 25 of My AI & Machine Learning Journey Today I learned about MultiIndex (Hierarchical Indexing) in Pandas — a powerful way to handle higher dimensional data. 🔹 What is MultiIndex? Normally: • Series → 1D (1 index needed) • DataFrame → 2D (row + column needed) 👉 But with MultiIndex, we can use multiple levels of indexing 🔹 MultiIndex in Series We can create multiple index levels Example: index = pd.MultiIndex.from_product( [['cse','ece'], [2019,2020,2021,2022]] ) s = pd.Series([1,2,3,4,5,6,7,8], index=index) 👉 Access data s[('cse', 2022)] s['ece'] 🔹 stack() & unstack() 👉 Convert between formats • unstack() → MultiIndex → DataFrame • stack() → DataFrame → MultiIndex 🔹 Why MultiIndex? 👉 Used to represent high-dimensional data in lower dimensions Example: 5D → 2D 10D → 2D 🔹 MultiIndex in DataFrame 👉 MultiIndex in Rows df.loc['cse'] 👉 MultiIndex in Columns df['delhi'] df['mumbai']['avg_package'] 🔹 MultiIndex in Both Rows & Columns 👉 Creates higher dimensional structure branch_df3 💡 To access a value → need multiple keys (row + column levels) 💡 Biggest Takeaway: MultiIndex helps manage complex, multi-dimensional data in a structured and readable way. #MachineLearning #Python #Pandas #DataScience #DataAnalysis #LearningJourney #AdvancedPython 🚀

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories