How do I use the MultiIndex in pandas?

  Рет қаралды 172,327

Data School

Data School

Күн бұрын

One of the most powerful features in pandas is multi-level indexing (or "hierarchical indexing"), which allows you to add extra dimensions to your Series or DataFrame objects. But when should you use a MultiIndex, and how do you create, slice, and merge MultiIndexed objects?
In this video, I'll demonstrate:
- How to create a Series with a MultiIndex, and how to convert it to a DataFrame
- How to select from a Series with a MultiIndex
- How to create a DataFrame with a MultiIndex
- How to select from a DataFrame with a MultiIndex
- How to merge two DataFrames with MultiIndexes
WANT TO JOIN MY NEXT WEBCAST? Become a member ($5/month):
/ dataschool
=== RELATED RESOURCES ===
Download the lesson notebook: nbviewer.jupyter.org/github/ju...
Using the pandas index (Part 1): • What do I need to know...
Using the pandas index (Part 2): • What do I need to know...
Analyzing groups with groupby: • When should I use a "g...
Selection and slicing with loc: • How do I select multip...
My full pandas video series: • Data analysis in Pytho...
DataCamp course on MultiIndex: www.datacamp.com/courses/mani...
DataCamp course on merging: www.datacamp.com/courses/merg...
Tidy data: r4ds.had.co.nz/tidy-data.html
== LET'S CONNECT! ==
Newsletter: www.dataschool.io/subscribe/
Twitter: / justmarkham
Facebook: / datascienceschool
LinkedIn: / justmarkham

Пікірлер: 314
@infiltr80r
@infiltr80r 4 жыл бұрын
It's rare to find exactly what you're looking for when it comes to Pandas but this was it.
@dataschool
@dataschool 4 жыл бұрын
Great to hear! 😄
@rezas2626
@rezas2626 5 жыл бұрын
Slice(None) trick was what I looked everywhere to find and found it here. Thank you so much!
@dataschool
@dataschool 5 жыл бұрын
You're very welcome! :)
@joaoostrowski
@joaoostrowski 5 жыл бұрын
Hello from Hungary Kevin! After a period away from the technicalities of python working mostly with Google Analytics and CRO techniques I'm glad to be back to your videos playing around Jupyter. I appreciate a lot the work you do on Data School, so here is a bit of my honest appreciation, thank you and keep it up! John Ostrowski :)
@dataschool
@dataschool 5 жыл бұрын
Thanks so much John! :)
@khardu
@khardu 2 жыл бұрын
Thank you for sharing your knowledge and experience beautifully. I am new to Python and Pandas, was struggling to understand how to select a subset of the data in a MultiIndex scenario. You made it a piece of cake by putting it all in one video, and by comparing and contrasting between Series and Data Frame.
@dataschool
@dataschool 2 жыл бұрын
That's awesome to hear! 🙌
@annieyang4420
@annieyang4420 4 жыл бұрын
Life saver video! i have been panic for 2 days , read many pieces and still confused. this helped me out! thank you!
@dataschool
@dataschool 4 ай бұрын
You're very welcome!
@carolinefawer-wasser2570
@carolinefawer-wasser2570 5 жыл бұрын
As always great explaination. You are a gifted teacher, so surely well worth the Patreon membership for you.
@dataschool
@dataschool 5 жыл бұрын
Thank you, Caroline, both for your kind words and for your support through Data School Insiders! :)
@stephenmaddock2863
@stephenmaddock2863 4 жыл бұрын
Brilliant tutorial. This is the 2nd of yours that I have watched and it covered what I need in my new job. Thank you very much.
@dataschool
@dataschool 3 жыл бұрын
You're very welcome!
@searchingsoul5910
@searchingsoul5910 4 жыл бұрын
Extremely informative, thanks for making it so simple - really good job
@dataschool
@dataschool 4 ай бұрын
Thank you! 🙏
@emuratspahic
@emuratspahic Жыл бұрын
Great explanation! Even though I used pandas a lot, I never really understood how multi-indexing worked. Thank you so much for this tutorial, this helped me out a lot!
@dataschool
@dataschool Жыл бұрын
Glad it was helpful!
@HD_157
@HD_157 5 жыл бұрын
The most helpful video I've ever seen about Python. Thanks a lot!
@dataschool
@dataschool 5 жыл бұрын
Awesome! Glad it was helpful to you :)
@carlomision9546
@carlomision9546 5 жыл бұрын
Clear explanation with examples. This is exactly what I needed to know. You have yourself a new subscriber.
@dataschool
@dataschool 5 жыл бұрын
Awesome! Thanks :)
@brockobama257
@brockobama257 2 жыл бұрын
I couldn't find the Corey Schafer video on MultiIndex so I'm here. And I'm not disappointed... subscribed!
@dataschool
@dataschool 2 жыл бұрын
Welcome to the channel! 👋
@billy818
@billy818 5 жыл бұрын
ive been stuck trying to understand this reading tru the documentation for ages. 10 mins and i get it this vid is great thanks
@dataschool
@dataschool 5 жыл бұрын
Awesome to hear!
@ssankarnarayan
@ssankarnarayan 3 жыл бұрын
slice(None) instead of : was a very important input. I struggled with it for quite a while before watching this video. Thank you.
@dataschool
@dataschool 3 жыл бұрын
You're very welcome!
@adamyork8407
@adamyork8407 5 жыл бұрын
This was so cool! I am already thinking of a dozen ways to use multi-indexing in my newbie projects and adding them to my little presentation next week. I owe a lot of my comfort with pandas to you. I hope your holidays went well, Kevin!
@dataschool
@dataschool 5 жыл бұрын
Awesome, thanks so much for your kind comments, Adam! 👌
@mahyarazad
@mahyarazad 4 жыл бұрын
Thank you gazillion, you are the best! I finally understand the functionality of the Groupby method!
@dataschool
@dataschool 4 жыл бұрын
Great to hear!
@shadrinan90
@shadrinan90 7 ай бұрын
Your lesson is sooo simple and clear. I found everything I wanted to know. Thank you
@dataschool
@dataschool 6 ай бұрын
Great to hear!
@Brocollipy
@Brocollipy 3 жыл бұрын
Thanks so much for this video. Really great to see a range of options with such clear advice.
@dataschool
@dataschool 3 жыл бұрын
You're very welcome!
@wiresandwavessolutions4723
@wiresandwavessolutions4723 4 жыл бұрын
Thanks for making this multi-index topic so easy for all of us. I understood, pivot table perfectly after this video. I am having difficulty in the following : I want to select all Symbols and calculate the difference between the closing price of all SYMBOLs from 4th to 05th .
@markcampanelli
@markcampanelli 5 жыл бұрын
Thanks a lot. Including slice(None) was very helpful.
@dataschool
@dataschool 5 жыл бұрын
Great to hear - you're very welcome!
@shabdarana2848
@shabdarana2848 5 жыл бұрын
Thanks. This topic had been bugging me but you make it seem really simple.
@dataschool
@dataschool 5 жыл бұрын
It is complex, but glad you are feeling good about it now!
@Mo3azSolomon
@Mo3azSolomon 9 ай бұрын
Thanks a lot for this simplified Explanation 😍
@dataschool
@dataschool 8 ай бұрын
You're very welcome!
@adarshrai22
@adarshrai22 Жыл бұрын
This is really a value adding video for data specialists. Thanks a lot for this brother.
@dataschool
@dataschool Жыл бұрын
Glad it was helpful!
@shrishailgajbhar3191
@shrishailgajbhar3191 Жыл бұрын
Thanks for helping us out with multiindexing in pandas...Cleared my daunting confusions about it.
@dataschool
@dataschool Жыл бұрын
Happy to help!
@owenlie
@owenlie 3 жыл бұрын
24 hours struggling to get the answer, and the answer just single line *unstack()* method. Thank you so much, I'll subscribe immediately!
@dataschool
@dataschool 4 ай бұрын
Great to hear!
@amitsardanayt
@amitsardanayt 2 жыл бұрын
You made this complicated taboo topic so simple to understand. Worth half an hour i spent
@dataschool
@dataschool 2 жыл бұрын
Thanks for your kind words!
@mdfaiz4583
@mdfaiz4583 5 жыл бұрын
u make things very simple....easy to understand ....thanks man
@dataschool
@dataschool 5 жыл бұрын
You're welcome!
@mkoc2071
@mkoc2071 5 жыл бұрын
Thanks for additional topics. Thanks for your time.
@dataschool
@dataschool 5 жыл бұрын
You're welcome!
@raminsadeghnasab9310
@raminsadeghnasab9310 Жыл бұрын
Finallllly, I understand how the multi-index works. Thanks man.
@dataschool
@dataschool Жыл бұрын
You're welcome!
@minglee5164
@minglee5164 3 жыл бұрын
Thanks for the excellent tutorial, I have been confused by this issue for a long time.
@dataschool
@dataschool 2 жыл бұрын
Glad it helped!
@MagicmathmandarinOrg
@MagicmathmandarinOrg 5 жыл бұрын
Thanks for the thorough explanation. Very helpful.
@dataschool
@dataschool 5 жыл бұрын
Great to hear!
@robertodilisio7535
@robertodilisio7535 5 жыл бұрын
Hi Mark! Great video on a very Syntax confusing matter such as multiindex. Thank you very much!
@dataschool
@dataschool 5 жыл бұрын
You're very welcome!
@mohammadkaifi
@mohammadkaifi 3 жыл бұрын
This is the solution I was looking for. Thanks!
@dataschool
@dataschool 4 ай бұрын
You're welcome!
@kilocesar
@kilocesar 2 ай бұрын
Great content man! Exactly what I needed!
@dataschool
@dataschool 2 ай бұрын
Great to hear! 🙌
@wildlife_and_drones1320
@wildlife_and_drones1320 3 жыл бұрын
Very helpful. I will be back with questions about machine learning.
@dataschool
@dataschool 3 жыл бұрын
Great! I've got a free course: courses.dataschool.io/introduction-to-machine-learning-with-scikit-learn
@akashpoudel571
@akashpoudel571 5 жыл бұрын
u r the bestest ever teacher to me.......damn lucid ua guideliness......thanks a tonne sir
@dataschool
@dataschool 5 жыл бұрын
Thank you! :)
@jsr7599
@jsr7599 3 жыл бұрын
Perfect video for what I needed. Thanks!
@dataschool
@dataschool 3 жыл бұрын
You're welcome!
@jerryetc
@jerryetc 3 жыл бұрын
Extremely clear, thank you
@dataschool
@dataschool 3 жыл бұрын
You're welcome!
@spiderschwein8g
@spiderschwein8g 5 жыл бұрын
Saved me hours. Thanks!
@dataschool
@dataschool 5 жыл бұрын
Great to hear!
@donaldbebel7979
@donaldbebel7979 3 жыл бұрын
Very good explanation of a confusing topic. Thanks you.
@dataschool
@dataschool 3 жыл бұрын
Glad it was helpful to you!
@pankajthakkar311
@pankajthakkar311 5 жыл бұрын
This is awesome, thanks for making this Video Kevin.
@dataschool
@dataschool 5 жыл бұрын
You are very welcome!
@ankitsajwan7520
@ankitsajwan7520 4 жыл бұрын
Exactly what I was looking for. Thanks :)
@dataschool
@dataschool 4 жыл бұрын
You're welcome!
@DeebzFromThe90s
@DeebzFromThe90s Жыл бұрын
Thank you sir. I will shoot you a donation if/when my quant journey presents some alpha. You are a lifesaver.
@dataschool
@dataschool Жыл бұрын
Thank you so much!
@rubencrespo1720
@rubencrespo1720 2 жыл бұрын
thank you so much, finally I could change from a Dataframe to a multiindex
@dataschool
@dataschool 2 жыл бұрын
You're very welcome!
@iloveno3
@iloveno3 5 жыл бұрын
Thank you. I have learned so much from you already...
@dataschool
@dataschool 5 жыл бұрын
Great to hear!
@ocamlmail
@ocamlmail Жыл бұрын
Great explanation, very clear, thank you!
@dataschool
@dataschool Жыл бұрын
Thanks!
@ahmadalrashedi5718
@ahmadalrashedi5718 3 жыл бұрын
man thank you soooooooooooooo much, I had been stuck on one thing for hours and finally figured it out!! !
@dataschool
@dataschool 2 жыл бұрын
You're so very welcome!
@5a035
@5a035 Жыл бұрын
19:50 "slice(None)", I never would have figured that out..... thank you!
@dataschool
@dataschool Жыл бұрын
You're welcome!
@amitgaj1
@amitgaj1 5 жыл бұрын
Thanks Kevin. That's brillant explanation.
@dataschool
@dataschool 5 жыл бұрын
Awesome! Thanks for your kind words :)
@ramvpage
@ramvpage 4 жыл бұрын
Excellent Tutorial ! Thank You!
@dataschool
@dataschool 4 жыл бұрын
You're welcome!
@mxmmalinin
@mxmmalinin 2 жыл бұрын
Thank you for sharing your knowledge!
@dataschool
@dataschool 2 жыл бұрын
My pleasure!
@SFayed-lg5sp
@SFayed-lg5sp 2 жыл бұрын
it's so helpful!!! thank you so much!
@dataschool
@dataschool 2 жыл бұрын
You're so welcome!
@ItsRainingSteak
@ItsRainingSteak 4 жыл бұрын
Top shelf content right here
@dataschool
@dataschool 4 жыл бұрын
Thank you!
@reibalachandran4775
@reibalachandran4775 4 жыл бұрын
Thanks for the video, I found it very useful and enjoyable to watch :)
@dataschool
@dataschool 4 жыл бұрын
Great to hear!
@CNW21
@CNW21 Жыл бұрын
As clear as day now , thank you
@dataschool
@dataschool Жыл бұрын
You're welcome!
@willd0g
@willd0g 3 жыл бұрын
Excellent - yes a few 💡 moments indeed. Very good
@dataschool
@dataschool 4 ай бұрын
Great to hear!
@florianrudowable
@florianrudowable 3 жыл бұрын
pandas is awesome and you are too! I cant belive that pandas is free and this video is free. Thank you.
@dataschool
@dataschool 2 жыл бұрын
Thank you so much! 😄
@olenamelnyk2427
@olenamelnyk2427 3 жыл бұрын
You saved my life with slice(None) (19:45). Thank you so much.
@dataschool
@dataschool 3 жыл бұрын
You're welcome!
@dmitku
@dmitku 3 жыл бұрын
Very useful as always. Many thanks!
@dataschool
@dataschool 3 жыл бұрын
My pleasure!
@jhonatangarcia3826
@jhonatangarcia3826 4 жыл бұрын
Thanks man! You helped me a lot!
@dataschool
@dataschool 4 жыл бұрын
Great to hear!
@krishj8011
@krishj8011 2 жыл бұрын
Awesome tutorial...thanks
@dataschool
@dataschool 2 жыл бұрын
You're welcome!
@user-dp1fz8gr8c
@user-dp1fz8gr8c 4 жыл бұрын
Thank you ! Your video is very helpful!
@dataschool
@dataschool 4 ай бұрын
You're welcome!
@szymonk.7237
@szymonk.7237 3 жыл бұрын
Thank you very much for this explanation ❤️
@dataschool
@dataschool 3 жыл бұрын
You're welcome 😊
@ebena.k.8032
@ebena.k.8032 2 жыл бұрын
Thank you so much. 'unstack()' has alleviated my headache.
@dataschool
@dataschool 2 жыл бұрын
You're welcome!
@chancemeeting9756
@chancemeeting9756 5 жыл бұрын
Super helpful, thanks!!
@dataschool
@dataschool 5 жыл бұрын
You're welcome!
@uncommon_common_man
@uncommon_common_man 4 жыл бұрын
great tutorial
@dataschool
@dataschool 4 жыл бұрын
Thank you!
@marvinjoyceburgstededijk9749
@marvinjoyceburgstededijk9749 3 жыл бұрын
Great video and many thanks. Greetings from Amsterdam
@dataschool
@dataschool 2 жыл бұрын
Thanks for watching!
@BookOfMorman
@BookOfMorman 3 жыл бұрын
This was great! Thank you!!!
@dataschool
@dataschool 3 жыл бұрын
You're so welcome!
@KarlosEPM
@KarlosEPM 2 жыл бұрын
Great video! Thanks :)
@dataschool
@dataschool 2 жыл бұрын
You're welcome!
@liamkholmes
@liamkholmes 3 жыл бұрын
Amazing! Thank you
@dataschool
@dataschool 2 жыл бұрын
You're welcome!
@pankushkukreja3101
@pankushkukreja3101 5 жыл бұрын
Thanks Alot, really a good series for pandas and easy to learn. I would request if you can make a video on Iteration in pandas like iterrows, itertuples and iteritems. Many Thanks In advance.
@dataschool
@dataschool 5 жыл бұрын
I think I cover it in this video: kzbin.info/www/bejne/eF7VaomrgJ1jms0
@prathameshmahankal4180
@prathameshmahankal4180 5 жыл бұрын
Thanks a lot for this video!
@dataschool
@dataschool 5 жыл бұрын
You're very welcome!
@semaphoreLock
@semaphoreLock 5 жыл бұрын
this is excellent. thanks
@dataschool
@dataschool 5 жыл бұрын
You're welcome!
@abcdefghi2650
@abcdefghi2650 3 жыл бұрын
THANK YOU BILLION TIMES!!!!
@dataschool
@dataschool 3 жыл бұрын
You're very welcome!
@dangantman5474
@dangantman5474 4 жыл бұрын
unstack() + reset_index() for the win!
@lathaprabakarg
@lathaprabakarg 5 жыл бұрын
very nice.. so useful
@dataschool
@dataschool 5 жыл бұрын
Thanks!
@zhenghaizhou6085
@zhenghaizhou6085 5 жыл бұрын
Great video!
@dataschool
@dataschool 5 жыл бұрын
Thanks!
@umeshdev6403
@umeshdev6403 2 жыл бұрын
Thank you. Nice explanation. how to get max values with the dates
@srosalia
@srosalia 4 жыл бұрын
Thank you!
@gggganzo
@gggganzo 2 жыл бұрын
Well explined. Would be great a video about data visualization with multiindex data structures also
@agaby18
@agaby18 5 жыл бұрын
Thank you! You saved me...
@dataschool
@dataschool 5 жыл бұрын
Great to hear!
@niva090
@niva090 3 жыл бұрын
Thank you, this helped me out so much! I have one question however, I hope you can answer for me here: I have a multi index with ID as the first and Day as the second. Now I want to select all ID's that has more than 6 days of data. How can I pass this logical statement to the selector?
@philtoa334
@philtoa334 2 жыл бұрын
Very nice.
@dataschool
@dataschool 2 жыл бұрын
Thank you!
@zennicliffzennicliff
@zennicliffzennicliff 5 жыл бұрын
Thank you so much. You are such a talented teacher. Where can I find out how I can structurally learn courses from you? Thank you and greetings!
@dataschool
@dataschool 5 жыл бұрын
Thanks! This post might be helpful to you: www.dataschool.io/launch-your-data-science-career-with-python/
@gowthamkumar6232
@gowthamkumar6232 4 жыл бұрын
Thanks!
@dataschool
@dataschool 4 жыл бұрын
You're welcome!
@fredsarti1
@fredsarti1 Жыл бұрын
Why slice(None).... omg. Thank you for your amazing video! Substribed
@dataschool
@dataschool Жыл бұрын
Thanks!
@alvaro8603
@alvaro8603 4 жыл бұрын
GREAT GREAT GREAT VIDEO!!!
@dataschool
@dataschool 4 жыл бұрын
Thank you!
@ramutillu5005
@ramutillu5005 4 жыл бұрын
Thanks alot. Very useful. Can u teach how to use multiple groupby in single expression.
@adelsayadkooh8870
@adelsayadkooh8870 4 жыл бұрын
thank you
@dataschool
@dataschool 4 жыл бұрын
You're welcome!
@asdkop7818
@asdkop7818 5 жыл бұрын
Nice video, anyway I would have liked a final section in which you could have explained the usage of logic over the selection on multi-indexes. Eg: what if I wanted to select data for each symbol and for each date except one? Is it possible to use the same logic as .loc with simple indexes? Thanks :)
@dataschool
@dataschool 5 жыл бұрын
Thanks for your suggestion!
@coolzkabhijit
@coolzkabhijit 3 жыл бұрын
Thanks for the video! I have a question. How to access a particular index without naming the index, like say without naming 'AAPL'
@rayli2221
@rayli2221 2 жыл бұрын
Thank you for sharing this information. I would like to ask if I wanted to get this to do portfolio optimization so calculating expected returns and covariance on multi-index dataframe how would you set this up?
@bhanuyadavyadav8870
@bhanuyadavyadav8870 5 жыл бұрын
Great videos may I request to show how to use .iloc with multi-index
@dataschool
@dataschool 5 жыл бұрын
Thanks for your suggestion!
@mountainscott5274
@mountainscott5274 4 жыл бұрын
Would you normally go through all of this? It seems like one can just stick to groupby() without complicating the syntax for selection or changing the shape of the dataframe. In other words, have you encountered multi-indexing often in the field?
@dataschool
@dataschool 4 жыл бұрын
Great question! Personally, I try not to use the MultiIndex very much. That being said, it is still useful to be able to work with it when you have to.
@marklittlewood2418
@marklittlewood2418 5 жыл бұрын
Nice presentation Kevin well done. How about accessing a range of dates for say apple, date:date ?
@dataschool
@dataschool 5 жыл бұрын
I'm not sure if that can be done using the colon, I'd have to check...
@mponcardas94
@mponcardas94 5 жыл бұрын
Wonderful!
@dataschool
@dataschool 5 жыл бұрын
Thanks!
@mounah2182
@mounah2182 3 жыл бұрын
Thanks for the video but i have a question how to merge columns and indexes in order to not have blank spaces in headers in my html view ?
@thimirabandara679
@thimirabandara679 3 жыл бұрын
Great video! is there a way to must index the column so there is an outer column and inner column? And if so how do you sort that? Because I know sort_column is not a callable function on the datafram
@surenderp2863
@surenderp2863 4 жыл бұрын
very good explanation in this vedio on multi-indexes, could you please explain, why do we need multi levels of columns and multilevels of row labels and again there is droplevel option, could you please explain on this.
@dataschool
@dataschool 4 жыл бұрын
The short answer is that you should use a MultiIndex if it helps you to represent the structure of the data.
@ridthrahman6637
@ridthrahman6637 4 жыл бұрын
How to use range selection (:) while inner and outer row labels are inside tuples? Use slice which probably did not allow range selection. Thank you!
@dataschool
@dataschool 4 жыл бұрын
Sorry, it's hard for me to say off-hand, good luck!
How do I merge DataFrames in pandas?
21:49
Data School
Рет қаралды 157 М.
How do I select multiple rows and columns from a pandas DataFrame?
21:47
Khó thế mà cũng làm được || How did the police do that? #shorts
01:00
Must-have gadget for every toilet! 🤩 #gadget
00:27
GiGaZoom
Рет қаралды 12 МЛН
МАМА И STANDOFF 2 😳 !FAKE GUN! #shorts
00:34
INNA SERG
Рет қаралды 4,3 МЛН
Retrieving from a multi-index in Pandas
11:58
Python and Pandas with Reuven Lerner
Рет қаралды 3,3 М.
How to Create Multi-Index DataFrame in Pandas
12:30
RegenerativeToday
Рет қаралды 14 М.
How do I make my pandas DataFrame smaller and faster?
19:06
Data School
Рет қаралды 66 М.
Stanford's FREE data science book and course are the best yet
4:52
Python Programmer
Рет қаралды 667 М.
Pandas MultiIndex Tutorial and Best Practices
30:15
Chicago Python Users Group
Рет қаралды 10 М.
My top 25 pandas tricks
27:38
Data School
Рет қаралды 265 М.
How to Reshape Dataframes | Pivot, Stack, Melt and More
12:49
Mısra Turp
Рет қаралды 23 М.
25 Nooby Pandas Coding Mistakes You Should NEVER make.
11:30
Rob Mulla
Рет қаралды 261 М.
How do I work with dates and times in pandas?
10:21
Data School
Рет қаралды 184 М.
Khó thế mà cũng làm được || How did the police do that? #shorts
01:00