Hi Reader,
Do you have a topic you'd like me to cover in a future tip? Reply and let me know! π
βDeep Learning Fundamentals (free course)
Are you interested in getting started with Deep Learning in Python? I haven't taken this course, but it looks promising!
I was thinking of starting a YouTube series in which I work through this course and share some of my learnings (and questions), and then you could follow along at home. Would that be of interest to you?
One aspect of Python's pandas library that can be confusing to newcomers is the concept of the "index". Every pandas DataFrame or Series has an index, and it's used for identification, selection, and alignment.
Here are some key facts about the index that will help you to work with pandas objects more effectively:
1οΈβ£ The index is on the left
The sequence of numbers on the left side is called the index, and they're used to identify the DataFrame rows. Sometimes they're called the "row labels".
2οΈβ£ By default, the index is a sequence of integers
Because I didn't define an index when creating the DataFrame, it assigned the numbers 0 through 192 as the index. They're stored in a special object called the "RangeIndex".
3οΈβ£ The column names are stored in an "Index" object
When people talk about "the index", they're talking about the row labels. But it turns out that the column names are stored in a special type of object called an "Index"!
That being said, you should never refer to the column names as "the index"... just call them "the columns" or "the column names".
4οΈβ£ The labels are not included in the shape
You should think of the index and columns as labels for the DataFrame. They're not part of the DataFrame "contents", and thus they're not included in its shape.
5οΈβ£ The index remains with each row
If you filter the DataFrame, the index remains with each row to identify it.
6οΈβ£ The index can be used for selection
You can use the loc accessor to select one or more rows or columns by label.
7οΈβ£ You can set a different column as the index
Notice that the default integer index no longer exists.
8οΈβ£ Setting a meaningful index allows for easier selection
Rather than referring to Brazil as "23" (its previous row label), you can refer to it as "Brazil".
If you want to learn more about the pandas index, check out these Data School videos:
π What do I need to know about the pandas index? (Part 1)β
π What do I need to know about the pandas index? (Part 2)β
Are you new to pandas? Stay tuned for my upcoming course, pandas in 30 days! Coming February 2024 π
Did you like this weekβs tip? Please forward it to a friend, or share this link in your favorite Slack group. It really helps me out! π
See you next Tuesday!
- Kevin
P.S. AI murder mysteryβ
Did someone AWESOME forward you this email? Sign up here to receive Data Science tips every week!
Join 25,000+ intelligent readers and receive AI tips every Tuesday!
Hi Reader, On Friday, I announced my forthcoming book, Master Machine Learning with scikit-learn. In response, my Dad asked me: How does the subject of this book relate to Artificial Intelligence? In other words: What's the difference between AI and Machine Learning? Ponder that question for a minute, then keep reading to find out how I answered my Dad... π AI vs Machine Learning Here's what I told my Dad: You can think of AI as a field dedicated to creating intelligent systems, and Machine...
Hi Reader, Yesterday, I posted this announcement on LinkedIn and Bluesky and X: Kevin Markham @justmarkham Dream unlocked: I'm publishing my first book! πππ It's called "Master Machine Learning with scikit-learn: A Practical Guide to Building Better Models with Python" Download the first 3 chapters right now: π https://dataschool.kit.com/mlbook π Thanks for your support π 1:47 PM β’ Sep 11, 2025 1 Retweets 5 Likes Read 1 replies This has been a dream of mine for many years, and I'm so excited...
Hi Reader, Hope youβve had a nice summer! βοΈ As for me, Iβve been finishing my first ever book! I canβt wait to tell you about it and invite you to be part of the launchβ¦ stay tuned π Today's email focuses on a single important topic: AIβs impact on your mental health π§ Read more below! π Sponsored by: Morning Brew The 5-Minute Newsletter That Makes Business Make Sense Business news doesn't have to be dry. Morning Brew gives you the biggest stories in business, tech, and finance with quick...