Latest / Talk Python To Me / #341: 25 Pandas Functions You Didn’t Know Existed
Transcript
- 0:00Do you do anything with Jupyter Notebooks?
- 0:02If you do, there's a very good chance you're working with the Pandas library.
- 0:05This is one of the primary tools for anyone doing computational work or data exploration with Python.
- 0:12Yet, this library is massive, and knowing the idiomatic way to use it can be hard to discover.
- 0:18That's why I've invited Beks Toichev to be our guest.
- 0:21He wrote an excellent article highlighting 25 idiomatic Pandas functions and properties
- 0:26we should all keep in our data toolkit.
- 0:28I'm sure there is something here for all of us to take away and use Pandas that much better.
- 0:33This is Talk Python To Me, episode 341, recorded November 4th, 2021.
- 0:39Welcome to Talk Python To Me, a weekly podcast on Python.
- 0:55This is your host, Michael Kennedy.
- 0:57Follow me on Twitter, where I'm @mkennedy, and keep up with the show and listen to past
- 1:01episodes at talkpython.fm.
- 1:03And follow the show on Twitter via at Talk Python.
- 1:06We've started streaming most of our episodes live on YouTube.
- 1:09Subscribe to our YouTube channel over at talkpython.fm/youtube to get notified about upcoming
- 1:15shows and be part of that episode.
- 1:17This episode is brought to you by Shortcut and Linode, and the transcripts are sponsored
- 1:23by Assembly AI.
- 1:24Bex, welcome to Talk Python To Me.
- 1:29Hello, Michael.
- 1:29Thanks for having me.
- 1:30Hey, it's fantastic to have you here on the show.
- 1:33Your article 25 Pandas functions that you didn't know or probably don't know, I guess, as we'll
- 1:40see.
- 1:41That really caught my attention.
- 1:43Honestly, I don't know many of them.
- 1:45So I learned a bunch by watching it.
- 1:47You know, I do spend more time on the web side of Python and the database side of Python than
- 1:53I do on the data science.
- 1:54But certainly Pandas is a super important part of Python these days.
- 2:00And honestly, the whole data science side is the fastest growing part of Python.
- 2:04Pandas is like one of the first libraries that you will be introduced in any beginner Python
- 2:09or in any beginner data science course.
- 2:12And it's amazing how much it has grown since it was first launched.
- 2:18And the funny thing about the article is that before writing it, I also didn't know most of the functions.
- 2:23I would always get annoyed by people who use some like complex functions.
- 2:28And I just wanted to know how they worked and explain it to my audience.
- 2:33So that was the idea of the article.
- 2:36Both me and the audience learning.
- 2:37That's the little bit of secret behind these types of things.
- 2:41Behind the tutorials, behind articles, behind podcasts, and even behind courses.
- 2:45A lot of times we dive into them because we're like, oh, I really want to learn these things.
- 2:50And just let me, you know, put it in a format I can present to the rest of the world and help
- 2:54everyone else out, right?
- 2:55Yeah, yeah.
- 2:56Awesome.
- 2:56Yeah, before we get into this, I want to talk about your articles and some Kaggle competitions.
- 3:01And then we'll dive into the 25 functions.
- 3:04But, you know, let's start with your story.
- 3:06How did you get into programming in Python?
- 3:07Right after I finished high school, I got interested in web development.
- 3:11I learned some HTML and CSS.
- 3:13And I was hoping to get things to get more, to be more exciting.
- 3:18But at some time, I just got bored because I'm really into math.
- 3:23And web development had nothing to do with math, so it was very boring.
- 3:28So I switched to learning Python.
- 3:30Learned it for a while and discovered that data science is more,
- 3:34mostly connected to math and statistics.
- 3:37So I just bought a really good course.
- 3:41And that's how it starts.
- 3:44Yeah, that's fantastic.
- 3:45You know, I think people do often feel like you have to be really good at math to be good
- 3:49at programming.
- 3:49And honestly, most of programming has very little to do with math.
- 3:54Yes, of course.
- 3:55Yeah.
- 3:56But data science does.
- 3:57So data science is unique in this way.
- 3:59I mean, I guess computational science, right?
- 4:01If you're an astrophysicist, you do a lot of math as well.
- 4:04But for most of us, math is just a structured way of thinking.
- 4:08And we have structured programs.
- 4:10And that's kind of the end of the relationship there.
- 4:12But if someone is out there and they really love math and they want to take it farther,
- 4:18but they want to do that in computers, it sounds like recommending data science might be the
- 4:21right path.
- 4:22Yeah, of course.
- 4:23It's very, really beautiful how software and math connect together in data science,
- 4:28what kind of things it can achieve for neural networks and state-of-the-art machine learning
- 4:34algorithms.
- 4:34It's really amazing.
- 4:36Yeah.
- 4:36It's one of these areas that's just growing so fast.
- 4:40And there's such big advancements.
- 4:42Yeah.
- 4:42You know, you look at, I think back to when I was in college and we talked about artificial
- 4:48intelligence and AI, and it was all about the Turing test, you know?
- 4:53Could you get a chat bot that would trick a human into thinking that it was an actual other
- 4:58human?
- 4:59And it never really seemed to come into reality.
- 5:03It always seemed like, oh, there's, it's kind of always 30 years out.
- 5:06And then all of a sudden we have self-driving cars and we have Google Copilot.
- 5:10Yeah.
- 5:11It's just the step jump over the last couple of years has been amazing.
- 5:14Yeah.
- 5:14I was also amazed by Google Copilot.
- 5:16Like right after it was launched, I wrote an article on it, like as a kind of intro.
- 5:21And it really took off.
- 5:23Like so many people were interested in it.
- 5:25Like it received like more than 50,000 views, the article.
- 5:28Yeah.
- 5:29A lot of people are amazed by it.
- 5:30I'm amazed by it as well.
- 5:31I think it's, it is amazing.
- 5:33It's also bringing to light some interesting, almost legal and philosophical things, right?
- 5:40If people put code on GitHub, they didn't necessarily intend to train an AI with it.
- 5:45If they put code on GitHub, that's under GPL.
- 5:48Well, what the AI knows, is that now GPL or is that completely, you know, can that be used
- 5:53in closed source?
- 5:54These are not known, right?
- 5:55These are, these are interesting questions.
- 5:57Yeah.
- 5:58I don't think we're going to answer.
- 5:59We're not going to completely fill them out today.
- 6:01Let's focus on something more, a little smaller.
- 6:05So you mentioned your articles and you've been doing a lot of writing.
- 6:09So you're a top 10 writer in artificial intelligence on Medium.
- 6:13Yeah.
- 6:13Yeah.
- 6:14Yeah.
- 6:14And you're also a Kaggle master.
- 6:17Yeah.
- 6:18Yeah.
- 6:18Yeah.
- 6:19Let's talk about those two things for a little bit.
- 6:20Just give us a sense of the stuff that you write about on Medium and maybe some of your
- 6:23favorite articles before we dive into this one that I picked out.
- 6:26I started writing on Medium a year ago.
- 6:29It was just purely for educational purposes.
- 6:31I really liked how like what the things you learn will be like, will be locked into your
- 6:37brain by writing about them.
- 6:39So it was a really amazing way to learn something new.
- 6:42But as my number of articles grew, like my audience grew and I met a lot of people.
- 6:49I had, it opened a lot of doors for me writing.
- 6:53Yeah.
- 6:54And most important of all, I'm more confident about my knowledge than ever before.
- 6:59That's fantastic.
- 7:00I really like that you point out that it opened doors because so many people feel like I'm
- 7:06not ready to write.
- 7:07I'm not ready to speak a user group or a conference, or I'm not ready to appear on a podcast or any
- 7:12of these sorts of ways where you put yourself out there.
- 7:14Right.
- 7:14Yeah.
- 7:15But when you do that, the act of doing that pushes you to grow.
- 7:19And it also opens doors to people.
- 7:21You know, if you're out there and you're genuine, you don't have to be an absolute expert in
- 7:25everything.
- 7:25You just have to be excited and interested.
- 7:27Other people who are excited want to talk to you and work on something with you, right?
- 7:30Yes.
- 7:31You just have to be one step ahead of your audience and that's it.
- 7:34Right.
- 7:34When you write articles.
- 7:35That's right.
- 7:36And not necessarily in everything they know, just the little area that you're interested in,
- 7:39right?
- 7:39Yes.
- 7:40Yes.
- 7:40Yeah.
- 7:40Awesome.
- 7:41And so that's really great that you're doing this writing stuff.
- 7:44The other thing is Kaggle.
- 7:45Tell us about what you've been doing at Kaggle.
- 7:48I really admire people who do, who do like do competitions on Kaggle for a while.
- 7:53And I really had this like imposter syndrome.
- 7:56I couldn't join the competitions because I thought that they were too complex that I had
- 8:01like a lot of things to learn before I joined them.
- 8:03I still do.
- 8:04But after I joined like the tabular playground competitions, I learned that I can do it.
- 8:11Yeah.
- 8:12So I started posting my articles in the form of notebooks on Kaggle as well, which started
- 8:17getting a lot of views and really nice comments from the audience.
- 8:20The community on Kaggle is even more amazing than on Medium.
- 8:24For an article that gets like read by thousands of people on Medium, I usually receive like
- 8:29one or two comments.
- 8:30But if you write, if you post the same article as a notebook on Kaggle, like the audience loves
- 8:35it because Kaggle is mostly suited for this kind of tutorials.
- 8:40And I usually receive like 30 or 40 comments.
- 8:42And that's really amazing as a writer to be part of that kind of community.
- 8:46Yeah.
- 8:47That's really amazing.
- 8:48I had no idea.
- 8:48I didn't realize you could post on Kaggle.
- 8:50Yeah.
- 8:51No.
- 8:51You kind of post your solutions and then have a conversation around them sort of, right?
- 8:55Yes.
- 8:55Yes.
- 8:55Yes.
- 8:56Okay.
- 8:56Awesome.
- 8:57People want to get started with Kaggle.
- 8:58What do they need to do?
- 8:59Like maybe before we drop this topic, if people haven't done stuff with Kaggle yet, but they
- 9:03maybe want to use it to learn.
- 9:04What's your advice there?
- 9:05Yeah.
- 9:06I just, right after you create an account, they have a whole suite of courses, free courses
- 9:12you can take.
- 9:13I think those are the, those are very best, very good starting points for any beginner.
- 9:19And also they have like two or three beginner level competitions.
- 9:23So you don't get intimidated by those grandmasters or masters.
- 9:27They're just a simple datasets you can work with and you just have to submit your predictions
- 9:33and just get a score and nothing too complex.
- 9:37And that's really the amazing part of Kaggle.
- 9:40That's why those three competitions I have, I think they have like 100,000 people competing
- 9:46at any single time in, in any time.
- 9:49That's wild.
- 9:50One of the challenges when you're learning is finding a structured problem to approach,
- 9:55right?
- 9:56Maybe in the web world, people try to build things that are too ambitious.
- 10:00They're like, oh, I want to build Airbnb.
- 10:02You're like, whoa, whoa, whoa, whoa.
- 10:03You don't really hardly understand CSS.
- 10:05Let's take it down a notch and let's go slow.
- 10:08And we'll get a right side problem for you to address.
- 10:10Data science has the same problem, but I think it has another aspect, which is, and you need
- 10:14the data to start from, right?
- 10:17Yeah.
- 10:17And I feel like Kaggle helps in bringing that kind of stuff over.
- 10:22Yeah.
- 10:22Yeah.
- 10:23Kaggle like has an amazing list of datasets.
- 10:26I almost always use Kaggle datasets for my, for my articles because most of them are digestible
- 10:33and small enough for people to get an advantage of.
- 10:35Awesome.
- 10:36A question from the audience from Brandon Bennett asks, are Kaggle competitions, just machine
- 10:42learning and artificial intelligence related?
- 10:44Are there other types?
- 10:45Yeah.
- 10:45Kaggle competitions are only AI or data science related.
- 10:49Yeah.
- 10:49Okay.
- 10:50So for example, the latest launched on Kaggle, I think is about finding the cuteness quotient
- 10:57of pets.
- 10:58It was, yeah, you just take in like thousands of images and you process them with Python or
- 11:05R and the neural network learns the structure and learns the cuteness quotient and just spits
- 11:12out a new quotient for any new image you get.
- 11:15That's amazing.
- 11:16So it used to be, here's a machine learning model that can answer, is it a cat or a dog?
- 11:21And now it's giving you a cuteness score.
- 11:23Yeah.
- 11:24Yeah.
- 11:24I can definitely see my daughter getting into data science with this one.
- 11:27She's all about pets and cats and dogs.
- 11:30And I personally want to put a vote out there for the golden cocker, the golden retriever mixed
- 11:37with the cocker spaniel.
- 11:38Boy, those things are cute.
- 11:39Okay.
- 11:40So that's Kaggle.
- 11:41Sounds really great for learning.
- 11:43Yeah.
- 11:43And I suspect knowing something about pandas will pay off.
- 11:47Oh, of course.
- 11:48Right?
- 11:49Like it's such a foundational aspect.
- 11:52Yeah.
- 11:52Pandas are used extensively.
- 11:55It is.
- 11:56And I feel like pandas is one of those things that you could learn it really quickly.
- 12:00You could learn to do stuff with pandas in a day.
- 12:03Yeah.
- 12:04Yeah.
- 12:05But then in a year, you could still be learning stuff about pandas.
- 12:08If you use it every day for a year, you know what I mean?
- 12:10Yeah.
- 12:11Most data science libraries are just very vast.
- 12:14There are a lot of functionalities.
- 12:16And most of the time, like you can get around by learning like 10 or 15% of all those functions.
- 12:22But when you really need to get something like really rare edge cases or unique cases,
- 12:28you really need to know some of those rare functions that are buried in the documentation
- 12:34just so that you don't have to reinvent the wheel.
- 12:37Yeah.
- 12:37In Python, we speak about Pythonic code.
- 12:41There's code that we could write that might be code that runs, but it looks like it comes
- 12:46from Java or it looks like it comes from C and somebody just got it working.
- 12:50And I suspect you have the same thing in data science and around pandas.
- 12:54It's like, yeah, you technically could do this with pandas, but why don't you just call
- 12:58this function?
- 12:59And probably the answer is, well, I didn't know that function existed.
- 13:02Of course, I would have called it if I had known to do it, but I just didn't know, right?
- 13:06I'm new.
- 13:06Yeah.
- 13:07So hopefully we can shine a light on some of those things that you can do.
- 13:10I mean, for example, not that we'll necessarily cover it in your article, but if you're doing
- 13:15a for loop with a data frame, you're probably doing it wrong, right?
- 13:18The golden rule is to never use loops, like teach loops completely.
- 13:23Yeah.
- 13:23That's pretty interesting.
- 13:24It definitely takes a different way of thinking, sort of set-based processing and passing in
- 13:30expressions and lambdas to various places and whatnot.
- 13:32Yeah.
- 13:33Maps and whatnot.
- 13:34Okay.
- 13:34We're going to talk about some of those.
- 13:35Let's dive in.
- 13:36First of all, how did you pick these 25?
- 13:39Were these just 25 that you saw people use?
- 13:41They were interesting.
- 13:42You're like, I didn't even know that existed.
- 13:43Or what was your philosophy here?
- 13:45For this kind of articles, I usually go to the API reference of the documentation.
- 13:49It just lists every single class and functionality of some library, the API reference.
- 13:54And I just read them one by one.
- 13:56I decide which one of those is going to be beneficial to me and possibly for my audience.
- 14:02And I just pick them out.
- 14:04Yeah.
- 14:04Come by one.
- 14:04Yeah.
- 14:05Yeah.
- 14:05That's really cool.
- 14:05I love to discover these types of things.
- 14:07So why don't we, you kick it off with number one?
- 14:10Yeah.
- 14:11What's number one here?
- 14:12The first one is Excel writer.
- 14:13It's a class for writing to Excel sheets.
- 14:18So if you have multiple data frames, you can write to Excel sheets as separate tabs with
- 14:24separate sheets.
- 14:24The pandas has usually, the data frames have this two Excel function, but you give it the
- 14:31Excel writer instance, it's going to write it to a separate sheet.
- 14:35It's going to enable you to write to separate sheets.
- 14:37Yeah.
- 14:38This is super neat.
- 14:38So in your example here, which of course we'll link to the article and people can check
- 14:43out, they all have a bunch of code samples under each one of these.
- 14:46You've got two data frames.
- 14:48Yeah.
- 14:48And you want to put them into some kind of Excel spreadsheet.
- 14:52So you create one of these writers.
- 14:54This is the function you're talking about.
- 14:56And then you go to the data frame, you say to Excel and you give it the writer and a sheet
- 14:59name and you give it, you can do that for each data frame and give it different sheet
- 15:03names and it just piles up along the bottom.
- 15:05Right.
- 15:05It's really neat.
- 15:06It's ridiculously simple, right?
- 15:08It's like given the data frames, it's three lines of code to create an Excel file and
- 15:12write it.
- 15:12Yeah.
- 15:13Yeah.
- 15:13If you know this, you'd have to create two separate Excel files and just add them together
- 15:18later manually, which is not programmatic.
- 15:20Right.
- 15:21Or maybe you say you don't know that you can write to Excel.
- 15:24I mean, I'm pretty sure I could write to CSV.
- 15:26Ah, yeah.
- 15:27And there's multiple levels, right?
- 15:28Like one level is like, I'm going to write it line by line, putting the commas in there
- 15:32myself.
- 15:33Another one could be the write CSV, right?
- 15:35Read CSV, write CSV.
- 15:36But this one is like more structured, right?
- 15:39And then you could possibly use some of the more advanced tooling to do things like stylize
- 15:44or highlight aspects of it or whatever, right?
- 15:47Like Py Open Excel or something like that.
- 15:49Now for this one, you talk about, all right.
- 15:52It says that you need to have the right supporting libraries there, right?
- 15:57You, for example, have to have different libraries.
- 16:00I can't remember which one it was.
- 16:01I think it was Py.
- 16:02Py open by Excel.
- 16:03Open by Excel.
- 16:04Yeah, that's it right here.
- 16:05I knew it was in here.
- 16:06Yeah.
- 16:06Open Py Excel.
- 16:07If you want to work with XLS files.
- 16:10And there's other ones as well, right?
- 16:12Otherwise, you'll get an error.
- 16:13Right.
- 16:14So basically, Pandas delegates to this library, which actually understands Excel and writes
- 16:20to it.
- 16:20Yeah.
- 16:21There's another one where it talks about using FSSpec.
- 16:25And this caught my attention as like, oh, wow, this is way more flexible.
- 16:29Because I'm not sure people are aware of what FSSpec is.
- 16:33Are you familiar with FSSpec?
- 16:34No, no.
- 16:35So FSSpec is this library that allows you to treat different destinations as Python file systems.
- 16:43Like, you know, with open some file name.
- 16:45Instead of file name, you can do all sorts of stuff.
- 16:48So let me see if I can find some of the documentation here of the things that it can go to.
- 16:54Yeah.
- 16:54Integrates with a bunch of different places, but it goes to places like S3 storage and
- 17:01FTP and database and zip files and all of these types of crazy things.
- 17:07And it even does caching, I guess, is right?
- 17:10So this Excel writer, while it already sounds really interesting because it writes to Excel,
- 17:15like destination of these Excel files, like this could be an Excel file in a database or
- 17:20something with basically hardly any changes to the code.
- 17:23Yes.
- 17:24Yeah, that's super cool.
- 17:25So good one to kick it off there.
- 17:27A lot going on.
- 17:28This portion of Talk Python To Me is brought to you by Shortcut, formerly known as clubhouse.io.
- 17:35Happy with your project management tool?
- 17:37Most tools are either too simple for a growing engineering team to manage everything,
- 17:41or way too complex for anyone to want to use them without constant prodding.
- 17:45Shortcut is different though, because it's worse.
- 17:48No, wait, no, I mean, it's better.
- 17:49Shortcut is project management built specifically for software teams.
- 17:53It's fast, intuitive, flexible, powerful, and many other nice positive adjectives.
- 17:58Key features include team-based workflows.
- 18:04Or customize them to match the way they work.
- 18:09type version control integration.
- 18:10Type version control integration.
- 18:10Type version control integration.
- 18:19Whether you use GitHub, GitLab, or Bitbucket, Clubhouse ties directly into them, so you can update progress from the command line.
- 18:26Keyboard friendly interface.
- 18:26Keyboard friendly interface.
- 18:28The rest of Shortcut is just as friendly as their power bar, allowing you to do virtually anything without touching your mouse.
- 18:34Throw that thing in the trash.
- 18:35Iteration planning.
- 18:36Set weekly priorities.
- 18:38Set weekly priorities and let Shortcut run the schedule for you with accompanying burndown charts and other reporting.
- 18:44Give it a try over at talk python.
- 18:47Talk python dot fm slash shortcut.
- 18:49Again, that's talk python dot fm slash shortcut.
- 18:53Choose shortcut because you shouldn't have to project manage your project management.
- 18:57The next one is pipe, right?
- 19:01Yeah.
- 19:02The image also.
- 19:03Yeah.
- 19:04There's like a lumberjack looking dude smoking pipe there.
- 19:07That's very cool.
- 19:08Yeah.
- 19:08Yes.
- 19:08Tell us about pipe.
- 19:09When you do data analysis, like most of the time, the data you'll be dealing with will be like not clean.
- 19:16You have to perform some operations.
- 19:18And pipe really offers a way to just package those all those operations into a single line of code or into a single code of block of code.
- 19:28It's kind of like it's going to like SQL and pipelines where you just have to run as only single line of code and just perform several operations at the same time.
- 19:37It's really just a neat way to do data cleaning.
- 19:40Right.
- 19:40And it's what's called a fluent API.
- 19:43So if I call data frame dot pipe, what comes back is another data frame and then I could call dot pipe on it again and then dot pipe and dot pipe and chain those together.
- 19:52Yes.
- 19:52Applying different operations and transformations.
- 19:55It's almost like a map reduce or aggregation framework type of thing here.
- 20:00Right.
- 20:00It's pretty flexible.
- 20:01I would think it's just like and it's, you know, its entirety like the amazing one of the amazing features of pandas like consistency always.
- 20:08Yeah, I really like it.
- 20:10It looks super neat.
- 20:11So you need to do transformations on a data frame with custom functions and get answers out.
- 20:17Yeah.
- 20:18Another thing that you pointed out here is that as part of this, you could apply it to the whole data frame or you could pass a set of columns.
- 20:26Yes.
- 20:26As part of it.
- 20:27So as what you're piping across, what does that do?
- 20:30That reduces the result to just those.
- 20:32If you pass in three things, just those three columns.
- 20:34And then these two functions remove outliers and go categoricals are our function that accept arguments.
- 20:41And when you pass it to pipe, we just have to pass the function name.
- 20:45Got it.
- 20:45Which means you can pass the arguments.
- 20:47So to pass the arguments, actually, you just have to provide them after the comma.
- 20:53So this remove outliers function just accepts one argument as a list and it performs like outlier removal and just returns the whole data frame.
- 21:02I see.
- 21:03So you can pass like your function might take the data frame, but it might also take additional information.
- 21:07Like I want to exclude things that are over a hundred dollars and just throw them away.
- 21:11Well, you got to pass that hundred in because it needs to know a hundred versus some other cutoff value.
- 21:16Right.
- 21:16Got it.
- 21:17Yes.
- 21:17Yes.
- 21:17Okay.
- 21:17Cool.
- 21:18And you say it resembles scikit-learn pipeline.
- 21:21Yeah, that's pretty cool.
- 21:23All right.
- 21:24We're up to number three factorize.
- 21:27Yeah.
- 21:28Tell us about this one.
- 21:29In machine learning as algorithms only accept numerical data and the most real world data sets contain categoricals, which means like there are like a class one, class two or class three.
- 21:41And you have to encode them like a two numeric, like zero, one, two, three, or using like one hundred encoder or labeling code in scikit-learn.
- 21:49But you can do that in pandas as well.
- 21:51You just have to pass the column to factorize and it just encodes them with numericals for each class.
- 21:58I see.
- 21:59So let me see if I can give an audio friendly example for listeners here.
- 22:03Yeah.
- 22:03If we've got something that says a data frame where one of the pieces is what the weather was like sunny, rainy, sun, rain, snow, clouds, something like that.
- 22:16You can't feed sun to the machine learning model.
- 22:20You got to give it a number, right?
- 22:21Yes.
- 22:21So this will convert that to like zero for sun and everywhere sun appeared, you would now have a zero.
- 22:26One for rain, everywhere there was a rain and so on.
- 22:30So it just does that, figures out how many different categories there are and then gives them a number that can be sent off to machine learning, right?
- 22:36You explained that.
- 22:37Awesome.
- 22:39You see, I'm learning, right?
- 22:40I'm just following along with you here.
- 22:42Awesome.
- 22:42Okay.
- 22:43That's a really cool one.
- 22:44This next one seems a little bit crazy, but it looks very useful.
- 22:48Explode, right?
- 22:49What is explode?
- 22:50Survey data.
- 22:51Surveys usually contain questions that are multiple choice.
- 22:55You can just pick a lot of like more than one answer to one question and that's recorded as one answer.
- 23:01So you're just going to end up with this kind of lists in a single cell of the table.
- 23:07Like a question.
- 23:08Oh, if you have a question one and the user just picks the answers ABC, it's going to end up ABC is going to end up as a list in a single cell of a table.
- 23:18Right.
- 23:18So for an example here, you have a series that has one and then six and then seven.
- 23:23And then the fourth element is a list of three other numbers.
- 23:26And you're like, wait a minute, those are not supposed to just be multidimensional.
- 23:30I want a straight series, right?
- 23:31You want a straight series.
- 23:32And when you call explode on this series, it's going to just expand the series vertically and just going to fill up.
- 23:40It just takes the elements of the single cell lists and just expands them vertically.
- 23:46Yeah.
- 23:47And these are the types of things that you were talking about with loops, right?
- 23:50It would be easy to go through and say, I'm going to build up a new data frame.
- 23:54And if I see a list instead of a number, I'm going to just start appending those from the list with an inner loop and then we'll carry on.
- 23:59Right.
- 23:59And here you've literally done it in one line.
- 24:01Yeah.
- 24:02Yeah.
- 24:02This would be crazy complex if you did it like manually.
- 24:04Right.
- 24:06And honestly, slower, right?
- 24:07Because a lot of this is probably implemented in C, whereas you would be doing it at the Python layer.
- 24:12It's going to be very slow.
- 24:13All right.
- 24:13Another question from Brandon out there.
- 24:15Glad he's here in the live stream.
- 24:16How would I apply Explode to the entire data frame?
- 24:20I'm guessing he's thinking about maybe if you had multiple columns and they each potentially had this.
- 24:26I don't think that's possible.
- 24:27Yeah.
- 24:27I don't think Pandas allows that.
- 24:29Yeah.
- 24:29Okay.
- 24:29So it's got to be on a series, not on a data frame.
- 24:32Right.
- 24:33Got it.
- 24:33Okay.
- 24:34Cool.
- 24:34So these are all fun names that stand out.
- 24:37The next one.
- 24:38You're a fun name.
- 24:39Yeah.
- 24:39Yeah.
- 24:40And you pick some cool pictures, right?
- 24:41Yeah.
- 24:42Yeah.
- 24:42All right.
- 24:42So what's the next one?
- 24:43Squeeze.
- 24:44Squeeze.
- 24:44Squeeze.
- 24:44As you can see, there are some conditional operators who return real data frames, even if it's a single cell.
- 24:53As you can see from the subset set, we're just asking the diamonds data frame to return all diamonds that are priced below $1.
- 25:02And it just returns a single result, which is 326.
- 25:08But it's returned as a data frame, which is not comfortable to work with, like a single cell data frame.
- 25:13Right.
- 25:13Because Panda doesn't know ahead of time that a .luck call is going to result in a single item.
- 25:18This happens a lot in databases, too.
- 25:20You do a query, and the result is actually a single thing.
- 25:23But the framework has no way to know that the data is structured in a way that's unique or that's a one thing.
- 25:29And I suspect that's common here with data frames as well.
- 25:32You're structured.
- 25:33Like, I know this is going to give me the one answer.
- 25:35Yeah.
- 25:36But it just returns the whole table.
- 25:38Yeah, yeah, you're like, well, now I got to, like, dig in and give me the first row, first column.
- 25:42Yeah, okay.
- 25:43So squeeze helps fix this?
- 25:44You just call this, like, oh, squeeze on a single cell data frame or series, and just it removes all the dimensionality and just returns the number.
- 25:53Interesting.
- 25:53That's cool.
- 25:53What happens if I call it on one that's got more than one item?
- 25:57Do you know?
- 25:58Does it just give you the first, or does it freak out and let you know?
- 26:01I never tried that.
- 26:02Yeah, I never tried that.
- 26:04Yeah, don't do that, right?
- 26:06It's like, maybe if you just actually want the first answer, maybe it's okay, but it also might give you an exception.
- 26:11I don't know.
- 26:12I'll be fine to try it now.
- 26:13Yeah, exactly.
- 26:14Cool.
- 26:14So the next one has to do with finding things in a range, right?
- 26:18Yeah, between, yeah.
- 26:20Yeah, it's like, just the name suggests, like, you want to take all the rows that are in between some range.
- 26:27For example, here in the code example, I'm choosing all diamonds that are priced between $3,500 and $3,700.
- 26:35Nice.
- 26:36So, of course, you could do this probably as an expression.
- 26:39You could definitely do this as a loop.
- 26:42But both of those are slower, I'm sure, because they're not implemented internally, right?
- 26:47Yeah, less elegant.
- 26:48This one is better and faster and shorter.
- 26:51Yeah, and one of the things, the third parameter you can pass here to between, in addition to, like, the lower bound and upper bound, is whether or not it includes the endpoints, right?
- 27:00This one is inclusive is neither.
- 27:03So it's like open set.
- 27:05Nice.
- 27:06Another thing that I've seen here, which is not one of your 25, but looks nice.
- 27:10I'm used to visualizing, quickly visualizing a data frame when I get it back with head or tail.
- 27:17And I want to know, like, okay, kind of what did I get back here?
- 27:19Show me the front.
- 27:20That'll be good.
- 27:21Do ahead.
- 27:21Or let's go to the end and see what happened at the end.
- 27:23But here you have .sample.
- 27:25That's interesting.
- 27:25I use it often because some data sets have, like, ordering, for example, time series data sets.
- 27:31And the first few rows might be not too representative of the whole data frame.
- 27:36So I just call sample with, like, five or ten rows.
- 27:40And that randomly samples the data set.
- 27:43And usually sometimes that represents the data set better than head or tail.
- 27:49Right, exactly.
- 27:50And so it just kind of randomly picks some stuff throughout the data set to show you what's going on, right?
- 27:55For large data sets, that's really handy.
- 27:57Nice to know.
- 27:58Yeah.
- 27:58So the next one has to do with, I'm guessing, like, when you're doing matrix multiplication and vectors and, like, truly doing math.
- 28:07Most of the time I would expect this to show up.
- 28:09Yeah.
- 28:10Most of the time, yes.
- 28:11Yeah.
- 28:11Transpose.
- 28:12Yeah.
- 28:12Yeah.
- 28:13It stands for transpose.
- 28:14I usually, you usually don't do math or matrix multiplication in Pandas.
- 28:19You will do it in NumPy.
- 28:21But this one, I use it mostly for when you, on the result of describe.
- 28:25You see here, describe returns.
- 28:28The axis inverted.
- 28:30So the five numbers number is given as rows.
- 28:33And that's really a problem when you have multiple columns because the data set starts to expand horizontally, which makes you scroll to the right, which you don't want.
- 28:45So when you do describe, you get things like given a data set, it'll say, here's the count of this index, the mean of this index, or this value of a column, standard deviation, and so on.
- 28:56And the number of options there is unbounded.
- 28:58But the fact that it goes count, mean, standard deviation, minimum, and then a few more things, that's fixed.
- 29:03And that fits pretty well.
- 29:04So you're saying if you transpose or flip the rows and columns so that you make it go vertical instead of across, that's an easier way to look at it.
- 29:12Yeah, yeah.
- 29:12I agree.
- 29:13And it's as easy as saying .t.
- 29:15So it's not too hard to do, right?
- 29:17You might as well.
- 29:17It's an attribute.
- 29:18Yeah.
- 29:19Cool.
- 29:19All right.
- 29:20That's a really good one.
- 29:22So you're saying if I'm going to do like some kind of matrix multiplication stuff, I should not do it in Pandas.
- 29:27I should just stick to NumPy.
- 29:28Yeah.
- 29:29NumPy is like purely for mathematical purposes.
- 29:31And it's much faster than Pandas.
- 29:34I suspect that NumPy has a good transpose as well.
- 29:36But yeah, there.
- 29:37It has the same attribute.
- 29:39Yeah.
- 29:39There's a lot of synergy between those two libraries.
- 29:41So the next one has to do with styling things and how they look, right?
- 29:45One of the things that's cool about Pandas is it mixes well with Jupyter Notebooks.
- 29:50And Jupyter Notebooks have a nice sort of explore the data.
- 29:53And let's see what's going on.
- 29:54Let me just look at it, right?
- 29:55So this styler thing, the style attribute helps you with that, right?
- 29:59Yeah.
- 30:00Here, like it takes advantage of that.
- 30:02The fact that Jupyter uses HTML and CSS under the hood.
- 30:06So you can take advantage of that and use some HTML and CSS knowledge to style your data frame based on some like hyphonic loops or conditionals.
- 30:17Here, for example, after you take the transports or the describe, you can just highlight the maximums of each row or column using the highlight column max function.
- 30:28Yeah.
- 30:29The Pandas offers a lot of functions after the style attribute.
- 30:32You can use the built-in functions or you can come up with some custom logic to style your data frame using HTML and CSS.
- 30:39Okay.
- 30:40Yeah, this is great.
- 30:41So you can say, for example, here dot style dot highlight max.
- 30:46And then you give it some CSS values like colors, dark red or something like that, right?
- 30:50You just don't have to look at the row numbers.
- 30:53It just shows you the most important metrics or the ones that you want.
- 30:57It's really useful when you have like multiple columns.
- 31:00You just don't want to have to.
- 31:01You just don't want to look at all those crazy numbers and you just use some.
- 31:06Yeah, like a real reasonable or maybe straightforward thing you might start out by doing.
- 31:11So, well, let me just sort it.
- 31:13We'll sort it so the highest one's at the top.
- 31:15But in this example, you've got multiple columns and the max of one column is in one value, but it's a different row for a different attribute of it, right?
- 31:24So sorting it is going to do nothing except for like if you come up with a whole bunch of variations and try to look at it and a little bit of color, a little bit of picture goes a long ways.
- 31:33Yeah, yeah.
- 31:34Visual.
- 31:34Yeah, absolutely.
- 31:35This portion of Talk Python To Me is sponsored by Linode.
- 31:40Cut your cloud bills in half with Linode's Linux virtual machines.
- 31:44Develop, deploy and scale your modern applications faster and easier.
- 31:49Whether you're developing a personal project or managing larger workloads, you deserve simple, affordable and accessible cloud computing solutions.
- 31:56Get started on Linode today with $100 in free credit for listeners of Talk Python.
- 32:01You can find all the details over at talkpython.fm/Linode.
- 32:05Linode has data centers around the world with the same simple and consistent pricing, regardless of location.
- 32:12Choose the data center that's nearest to you.
- 32:15You also receive 24, 7, 365 human support with no tiers or handoffs, regardless of your plan size.
- 32:23Imagine that real human support for everyone.
- 32:25You can choose shared or dedicated compute instances, or you can use your $100 in credit on S3 compatible object storage, managed Kubernetes clusters and more.
- 32:36If it runs on Linux, it runs on Linode.
- 32:38Visit talkpython.fm and click the create free account button to get started.
- 32:43You can also find the link right in your podcast player show notes.
- 32:46Thank you to Linode for supporting Talk Python.
- 32:49Yeah, the second example you have here in your article is a little more nuanced.
- 32:55This looks great.
- 32:55Tell us about that.
- 32:56This one is like background gradient.
- 32:58So it just colors each cell of the column based on its magnitude.
- 33:04It's kind of like a continuous palette.
- 33:08It just shows where the maximum or the minimums are and just how they compare to each other.
- 33:15Yeah, it's almost like if you could do a heat map in an Excel table, you know, by making the cells different colors.
- 33:20You can pass in a color map and all sorts of stuff to control how that looks.
- 33:24Yeah.
- 33:24Yeah, cool.
- 33:25I like it.
- 33:25This is great.
- 33:26You know, it's one of these things where, again, one line of code and you can dramatically improve the presentation value or the informational value of what you're looking at.
- 33:34Right.
- 33:35Nice.
- 33:35All right.
- 33:36I feel like that's similar to your number nine.
- 33:39Yeah.
- 33:39This one is Pandas options.
- 33:41Like it's kind of settings of your phone.
- 33:43You just set them globally and it applies to all the data frames, the series and all the functions that you are going to be using inside the project or inside the session of Jupyter Notebook.
- 33:53So if you want to have some sort of number of columns that are shown or some kind of color or something like that, you can just set that up at the beginning.
- 34:01Yeah.
- 34:01You just don't have to call them every single time or change them every single time.
- 34:05It's just a shorthand of way of doing things like setting global settings.
- 34:11Yeah.
- 34:11You could probably even do something like have a little JSON file that describes the look and feel of what you're doing.
- 34:17Just your first line, just load it up and set it and then, you know, go from there.
- 34:21Something to that effect, right?
- 34:22Yeah.
- 34:23Yeah.
- 34:23So you don't have to completely fill the first few lines of your notebook with like setup code.
- 34:28Yeah.
- 34:28For example, one of those examples is like a display max rows.
- 34:32If you set it to five and you just call the data frame, it's going to only show the first five rows.
- 34:38So you don't have to call .hat every time.
- 34:40Oh, that's interesting.
- 34:41Yeah.
- 34:42Because of course, if there's enough rows, it won't print the whole thing out, right?
- 34:45Probably.
- 34:46Yeah.
- 34:46You don't want to print 10 million rows and completely lock up the system.
- 34:49Yeah.
- 34:50Yeah.
- 34:50Yeah.
- 34:50Yeah.
- 34:50That's going to.
- 34:51Cool.
- 34:52Oh, and another one that's kind of nice is display precision.
- 34:55And if you set that, you won't see the, you know, 1.27e to the five or whatever, right?
- 35:01You can.
- 35:02It's really annoying when you're working with like math functions.
- 35:06It just keeps giving in like scientific notation when you just want to like see the first or four or five decimal places.
- 35:15Yeah.
- 35:15Scientific notation is great when you're dealing with huge numbers or tremendously small numbers, right?
- 35:20Like how many meters across is an atom?
- 35:23Okay.
- 35:23So you're going to need an E to something.
- 35:25But for human beings often, you know, you want to just look at the number and go, yeah, that's a million, not like, you know, 1.2e to the six or seven, whatever.
- 35:33It's going to be really annoying.
- 35:34That's cool.
- 35:35And this is just one of those options you can set up and it just globally applies to that notebook.
- 35:39So another thing that's interesting about pandas is the columns have types usually, but not always.
- 35:46It's one of those like beginning level things that you will encounter, but it can get really annoying if the data types are incorrect for your column.
- 35:55The most important one is the object data type.
- 35:58Right.
- 35:58That's like, I don't really know.
- 36:00So we're just going to store it.
- 36:02Yeah.
- 36:02Yeah.
- 36:03I'm just going to put it inside of an object and objects are like object data type is the worst one.
- 36:10It also limits the functionality of pandas and it's also the most memory consuming.
- 36:14Right.
- 36:15So the next function, what number are we on here?
- 36:18Number 10.
- 36:1810, yes.
- 36:19And the hit list is convert underscore D types as in convert data types.
- 36:25When you call it on the whole data frame, it just, it tries to infer the correct data type for each column.
- 36:32If it's a float or integer or string like that.
- 36:36So your example, you're reading a CSV file and some of the columns are detected correctly like floats, but others get this object.
- 36:43But after calling convert D types, it's like, you know what?
- 36:45No, those are strings.
- 36:46But it can't handle the date times because there are so many date time formats and pandas can't possibly know all of them.
- 36:54Why are date times so hard?
- 36:55They really shouldn't be, but they really are.
- 36:58It's crazy.
- 36:58And then you throw in time zones and you'll forget it.
- 37:01Okay.
- 37:01And throw in daylight savings and all these other things.
- 37:04Oh, yeah.
- 37:05That's crazy.
- 37:06Yeah.
- 37:07Daylight saving is crazy.
- 37:08Yeah.
- 37:08I suspect some of the Kaggle stuff.
- 37:10Part of the challenge is like normalize these dates because who knows or something along those lines.
- 37:15Time zones are like total mess.
- 37:18Yeah, for sure.
- 37:18So related to converting the data types is to select them.
- 37:24Yeah.
- 37:24Which is a way to filter what's in there.
- 37:28Like you can filter by column or rows or even a condition.
- 37:31But this is saying like, I only want the strings or only want the numbers, right?
- 37:36While doing machine learning, you have to apply certain pre-processing functions to only a subsets of the data.
- 37:43Like only on categoricals or only numerics.
- 37:46So this function will become very handy.
- 37:49You just pass the data type using NumPy.
- 37:54And it just gives all the subset of the data frame with that data type.
- 37:58Nice.
- 37:59So you would say like data frame dot select data types and then include equals np number.
- 38:04And now instantly the resulting data frame is a subset that only has numbers, right?
- 38:09Yes.
- 38:10That's cool.
- 38:10And then also you point out that you can do the reverse.
- 38:12Just like give you just the other, like just the informational bits, like categories and stuff or rating by saying exclude.
- 38:20Yeah.
- 38:21Very nice.
- 38:21Okay.
- 38:22Well, we just missed it with Halloween here.
- 38:24Yeah.
- 38:25Yeah.
- 38:26Mask.
- 38:26But mask.
- 38:27Yeah.
- 38:28Cool.
- 38:28Like a mask here.
- 38:30But mask is number 12.
- 38:33That's about it.
- 38:34It's a conditional on, you can use it on, on series or data frames and it just returns the subset of the data where some condition is true.
- 38:44Yeah.
- 38:45Okay.
- 38:45So, yeah.
- 38:46And this example here, you've got a bunch of ages.
- 38:49And I want to subset them using B2.
- 38:51I want to take all those rows that are beyond 60 or below 50 and convert those values to NAND.
- 38:59Okay.
- 38:59So, this is like an in-place update or I guess it replaces, creates another one that is like as if you updated it.
- 39:06And it finds all the stuff that's, I guess, outside of your range and then applies this other value, right?
- 39:12Like if it's stuff that's outside of this range, in this case, you're going to set it to not a number, but it could be set to zero or max or anything.
- 39:19Uh-huh.
- 39:20Yeah.
- 39:20Cool.
- 39:21A very good one.
- 39:22Similar, I guess, is min and max.
- 39:25And then some of these, as we get a little farther down your recommendations, I like them.
- 39:28They're not just, oh, here, you can apply this function, but apply it in this scenario or this context to get an interesting outcome, right?
- 39:36So, that's what number 13 is like.
- 39:37Min and max along columns axis.
- 39:40Usually, when you call min or max on a column, it just returns the minimum or maximum of that column.
- 39:47But sometimes you want it to row-wise, like it just treats rows as columns and it gives min and max across the rows.
- 39:56That's usually useful.
- 39:58A handy way of doing something that would take a lot of code if you're done manually.
- 40:02Another one of these tricks that are techniques that lets you avoid looping, right?
- 40:06Here I show a good example of like comparing four different libraries on five datasets.
- 40:11You want the best performance on each dataset.
- 40:14So, you have to find the best score across the rows.
- 40:18Exactly.
- 40:18So, the columns are the different libraries like XGBoost, CatBoost, scikit-learn, and so on, being applied to the same dataset.
- 40:25And you want to just go for row one, what one did the best?
- 40:28Row two, what one did the best?
- 40:29Yeah.
- 40:30Yeah.
- 40:30Very nice.
- 40:31It takes a lot of code if done manually.
- 40:32Yeah.
- 40:33Cool.
- 40:33Number 14, N largest and N smallest.
- 40:37Yeah.
- 40:38We're talking about those max or minimums.
- 40:41So, N largest, when you pass a number and a column name, it just returns the data frame that contains the smallest or largest N rows of that column.
- 40:53Nice.
- 40:53So, if I were to call min or max, that would give me the smallest or the largest one, respectively, right?
- 40:59Yes.
- 40:59But a really interesting or common question you might have is like, what are the top 10 selling products this month, right?
- 41:07Yeah.
- 41:07And this lets you just say N largest 10, and then you pick the column on which to judge it.
- 41:12Here you have price, right?
- 41:13Five most expensive diamonds in the diamonds data set.
- 41:16Yeah.
- 41:16Again, one of these things that, you know, no more looping or any of that stuff.
- 41:20No more if statements.
- 41:21Just call it, right?
- 41:22This one is like the five cheapest, most cheapest diamonds.
- 41:25Yeah.
- 41:25And so, N smallest and N largest.
- 41:27Fantastic.
- 41:28Also, sometimes when you're asking for a minimum or maximum thing, you don't actually want the minimum or maximum.
- 41:34You want to know where that is because you're going to get that thing back and say, I need that whole row because I want to learn more information about it, right?
- 41:42But if you said, well, what's the minimum price?
- 41:44It's seven.
- 41:44Like, oh, okay, great.
- 41:46Now do I need to like loop through until I find that thing that has seven or something like this?
- 41:50So, you've got a recommendation for that.
- 41:52Yeah.
- 41:52The IDX man is IDX min.
- 41:54This returns the index values of minimum or max.
- 41:58So that you can look at the row that they are stored at or the column.
- 42:03Fantastic.
- 42:03Yeah.
- 42:04So, here's the row that contains the minimum price.
- 42:07I love it.
- 42:08Yeah.
- 42:08Really nice.
- 42:08So, so many of these are really easy to apply, right?
- 42:11Like, it's not a lot of research to learn how to apply ID max, but at the same time, or IDX max, but at the same time, knowing that it exists, now all of a sudden you can use it really easily.
- 42:22But you probably wouldn't have known to look for it, right?
- 42:24Yeah.
- 42:25Yeah.
- 42:25Cool.
- 42:26People often talk about differences between beginner developers and expert developers.
- 42:32And I think a lot of times beginners look at folks like you who have a lot of experience.
- 42:36They're like, oh, this guy is so incredibly smart and he just has this way of solving these problems.
- 42:40It's so amazing.
- 42:41And, you know, to some degree, that's probably true.
- 42:44But a lot of it is like just building up layers and layers of these like, oh, I know I can use ID max, IDX max.
- 42:51I know that I can use N largest.
- 42:53And you just sort of pile them together.
- 42:54And then like, bam, like the solution becomes easier because you have these little building blocks.
- 42:59Right.
- 42:59So it's, I think it's really valuable for people getting into Pandas.
- 43:03I usually think that the biggest difference between a beginner level and a more experienced programmer is just, is like just how much time they spend on the documentation.
- 43:12Yeah.
- 43:13Yeah.
- 43:14Yeah.
- 43:14If you read the docs, like if you patiently read the docs, you're just going to become a really good user of that particular tool or library.
- 43:21I agree.
- 43:21There's just more, you understand it better.
- 43:24You know more of what it has to offer.
- 43:26So it's like, it's less you've got to reinvent.
- 43:29Yeah.
- 43:29All right.
- 43:29I talked about how you have something that may be well known, but then applying it in a scenario.
- 43:34And this number 16 is value counts with drop in a false.
- 43:39What's this one about?
- 43:40When you have a series with like categoricals, you just want to see the proportions or their numbers as a whole in the total series.
- 43:48And that usually doesn't include the null values.
- 43:51So you have to call is null and chain it with some so that you get a, you learn the number of NADs in that column.
- 43:59But you can do it efficiently with value counts with setting, by setting drop in a to false, which includes the proportions of the null values as well.
- 44:09Yeah.
- 44:09So it just gives you a, basically a percentage as a ratio here.
- 44:13It's just a ratio of the number of the different categories that have appeared here.
- 44:18Right.
- 44:19So very cool.
- 44:20And now just not a number is included.
- 44:21That's great.
- 44:22Yeah.
- 44:22Number 17 clip.
- 44:24This is a good one.
- 44:25Yeah.
- 44:25Yeah.
- 44:25For data that exceeds, I don't know, maybe a range, maybe it's supposed, some instruments supposed to collect zero to a hundred and it's goes crazy and goes outside of a hundred.
- 44:34Yeah.
- 44:35For example, we go back to the ages example where I just want to have ages between like 18 or 60, 18 and 60.
- 44:43And I want to exclude all those values.
- 44:46And when you call clip with those custom values, it's just going to impose those hard limits on the whole series.
- 44:52Right.
- 44:52So it'll replace the ones that are over with the maximum that you said and the ones that are too low, it'll bring them up to the minimum.
- 44:59Right.
- 44:59Yeah.
- 44:59Very cool.
- 45:00Again, against the whole data set, not looping.
- 45:02Only at one column at a time.
- 45:04Yeah.
- 45:05We talked about how difficult time is, but you do have some recommendations for searching for data that appears at a certain time or in a time range, right?
- 45:14What's number 18?
- 45:15This one is like a subsetting of rows of the data frame at some particular time of the day, like any time of the day.
- 45:23But you like, for example, three o'clock, 9.30, 10.30, or any time that you want.
- 45:29You're just going to take all those rows and return them using at time.
- 45:33Yeah, that's super easy, right?
- 45:35Just pass in at time and you literally specify times, right?
- 45:39Like 15 colon zero, zero as a string.
- 45:42Like a real conversation or messaging.
- 45:46And then the other one, which is also interesting, is between time, right?
- 45:49Like what happened in the morning, for example?
- 45:52Like what are those sales that happened in the morning or after midnight or during some particular interval?
- 46:00This one is really handy to do that.
- 46:03Yeah.
- 46:03So super easy.
- 46:04Just data frame dot between time.
- 46:05Or is that a series?
- 46:06No, it doesn't matter.
- 46:08Okay.
- 46:08It doesn't matter.
- 46:09It usually has to be, it has to have a daytime index.
- 46:13That's it.
- 46:13Yeah.
- 46:13Okay.
- 46:14So then you just pass in strings like 9 colon 45 to 12 colon zero zero.
- 46:19And you know, that's like late morning or something.
- 46:21Beautiful.
- 46:21The next one here has to do with time series.
- 46:24Number 19, B date range.
- 46:26Tell us about this.
- 46:27Well, this one is like, stands for business date range, business date range.
- 46:32So like fundus internally built in a lot built into calendars.
- 46:37Like it just, when you want to, how can I say, when you want to index the data frame, you want time series data frame.
- 46:43You want to include only like working days.
- 46:47Like you want to exclude all the weekdays, weekends.
- 46:49Yeah.
- 46:50You can do that for every single of the year or for every single week of the year, because you can possibly know which days are weekends.
- 46:57So when you call B date range, it just takes, it just indexes the data frame using only weekdays.
- 47:05And also it excludes the holidays, I think.
- 47:09Oh my gosh.
- 47:09I was just wondering about holidays.
- 47:11Like there's another wrinkle in there.
- 47:12Already things like leap year and stuff like that is built into this, I would imagine.
- 47:17So this is super cool.
- 47:18Yeah.
- 47:19This is very important for when you are doing time series forecasting or announcing analysis because like, or working with stocks because stocks are only traded on weekdays and not on holidays.
- 47:31So it will be very important.
- 47:32Or even if you do in like traffic analysis, you want to understand accidents that are a result of rush hour, right?
- 47:39You wouldn't want to look on a weekend.
- 47:40Yeah.
- 47:41All right.
- 47:41The next one has to do with correlation.
- 47:43Auto core, C-O-R-R.
- 47:46Yeah.
- 47:46Auto correlation.
- 47:47Yeah.
- 47:48I don't do much with time series.
- 47:49You're going to have to tell us about this one.
- 47:51What's going on here?
- 47:51This is usually how it's the auto correlation of a series or time series tells the predictability of the time series with itself.
- 48:00It's, do you know about correlation coefficient?
- 48:03Yeah, exactly.
- 48:03It tells you how much the model matches the actual data.
- 48:07Like it's 97% likely that the model will predict the stuff coming up, right?
- 48:12Could be linear or more complicated, but that's something like that.
- 48:14Yeah.
- 48:14Yeah.
- 48:15The gist of this is that if a time series has a high auto correlation with itself, it means that you can predict it more easily.
- 48:23Got it.
- 48:23Yeah.
- 48:23It's basically how predictable or unpredictable is this thing.
- 48:27Yeah.
- 48:27There's a lot of details about autoc relation and it has very many applications in time series.
- 48:34But the gist is that like it shows you how much predictability it has like at each interval.
- 48:40Cool.
- 48:40It sounds very useful if you're doing that kind of stuff.
- 48:42All right.
- 48:43Number 21 has NANDs.
- 48:45It's also an attribute.
- 48:47You just call it on a series and it returns true or false.
- 48:51If you have, it returns true if you have at least one missing value in a series.
- 48:56Yeah.
- 48:56So there was this quote, I remember who it's attributed to.
- 48:59Sorry.
- 49:00That says something to the effect of like data cleanup and data wrangling is not the dirty work.
- 49:06It is the work of data science, like to get everything ready.
- 49:09And then you just like hit it with the magic at the end.
- 49:11Right.
- 49:12And this feels like that lands right in that realm is like given some data frame or series,
- 49:16does it have not in numbers or is it all good?
- 49:19Yeah.
- 49:19Missing values is like a huge problem in machine learning.
- 49:22Most scikit-learn algorithms don't accept missing values.
- 49:26So you either have to drop them or impute them using some techniques.
- 49:31And this one is very handy to detect those missing values.
- 49:35Right.
- 49:36I suspect this is the first test.
- 49:37Like if it has not in numbers and then we're going to go do stuff.
- 49:41But if it says false, then you're good to go.
- 49:43Just roll.
- 49:43Yeah.
- 49:44Yeah.
- 49:44Go with that.
- 49:45But it usually turns through.
- 49:46Unfortunately.
- 49:48Are you familiar with the missing no?
- 49:51Let me.
- 49:51Yeah.
- 49:52Yeah.
- 49:52This is another thing that I would sort of came to mind is like this whole thing, this
- 49:57missing no package as in like no numbers.
- 50:00So a way to not just answer yes or no, but to get visualizations.
- 50:03Have you used this?
- 50:04Yeah.
- 50:05Yeah.
- 50:05I also wrote an article on it, I think.
- 50:07Okay.
- 50:07Well, yeah.
- 50:08So definitely.
- 50:08That's awesome.
- 50:09Yeah.
- 50:09Things like this sound really useful to me.
- 50:11They seem like.
- 50:12I really like that missingness matrix.
- 50:14It just shows the reasons why missing values are correlated to how missing values are correlated
- 50:20with other columns.
- 50:21Right.
- 50:21Is it a whole bunch of missing data in one row?
- 50:23Yeah.
- 50:24And then it's all good?
- 50:25Or is it interspersed?
- 50:26Like this one's missing the birthday, but that one's missing the name or something like
- 50:29that.
- 50:29Right.
- 50:30Yeah.
- 50:30It's a really good package.
- 50:31Yeah.
- 50:31Fantastic.
- 50:32All right.
- 50:32At number 22.
- 50:34At and Iat.
- 50:35This one is like a faster versions of lock and Iat.
- 50:39It just enables you to index your data frame.
- 50:43But this one is specifically designed for retrieving single value conditionals.
- 50:48Nice.
- 50:48It's almost like an array index.
- 50:51Yeah.
- 50:51A little bit.
- 50:52What's the difference between at and Iat?
- 50:54Using at, you can use like column labels.
- 50:57Like as you can see here, we are using cut and an index.
- 51:00But Iat, you have to know the index of that column.
- 51:03I see.
- 51:04So with At, it would be like row and then column name, where Iat is row and column number.
- 51:09It's probably less flexible.
- 51:10You got to know that cut is four because it could be moved around as people are creating
- 51:15or inserting data.
- 51:16Yeah.
- 51:16Okay.
- 51:16Ag sort as in aggregation.
- 51:19This one just returns the indices that would sort a data frame.
- 51:23Okay.
- 51:23Based on some column.
- 51:25So in during data analysis, you sometimes want the indices, not the actual sorted data
- 51:31so that you can use those indices in multiple times over.
- 51:34Got it.
- 51:35So you get the sorted.
- 51:35Say, I want to sort by the total bill.
- 51:38Yeah.
- 51:38But then give me the indexes as if it was sorted, but don't actually change it.
- 51:43So then you could go and then request data off those indexes.
- 51:46Got it.
- 51:46Yeah.
- 51:46Yeah.
- 51:47Nice.
- 51:47All right.
- 51:47We're closing in on the end and we've brought in the cat, the cat accessor.
- 51:51Cat accessor.
- 51:52Yeah.
- 51:52I should have put an image here.
- 51:54Yeah.
- 51:54There would have been some kind of cool cat you can put in there.
- 51:57Yeah.
- 51:58As like pandas enables you to perform some like data type specific functions.
- 52:03Like there is DT accessor for date time and also STR for strings.
- 52:08And this one is for strictly for categorical purposes.
- 52:11It has like a large suite of categorical functions that makes it easier to work on categories, ordinals
- 52:19or nominal data.
- 52:20Yeah.
- 52:21Fantastic.
- 52:22And let's bring it to the 25th with a nth group by nth.
- 52:27Yeah.
- 52:27This one is less useful or used very in very rare edge cases.
- 52:32When you group by some column, possibly a categorical column, we want to look at those rows or groups,
- 52:38right?
- 52:39Calling nth on grouped data frame just returns that nth row or nth row of that groups of each group.
- 52:47Got it.
- 52:47Okay.
- 52:48Yeah.
- 52:49That looks really cool.
- 52:49Yeah.
- 52:50All right.
- 52:50Well, that's it for our list.
- 52:52Hopefully people out there listening have definitely learned something.
- 52:56Now, your title was just to put a little disclaimer in here for everyone.
- 53:00It's 25 panda functions you didn't know existed.
- 53:02Pipe P guarantee equals 0.8.
- 53:04So you had this 80%.
- 53:06Yeah.
- 53:06I'm guaranteed.
- 53:07I love it.
- 53:08That's a little bit of a stats joke in the title.
- 53:10No one complained about that.
- 53:12So I think that was right.
- 53:13Yeah.
- 53:14It sounds about right.
- 53:15It seems like there's a lot of neat use cases here that people can find.
- 53:18These are your 25 that you found interesting.
- 53:20Other people might find them as well.
- 53:22There are so many.
- 53:23Oh, so many.
- 53:24Yeah.
- 53:25These are the types of things, though, that people can say, all right, today I'm going to
- 53:29try to work with number one as I'm doing my data analysis and stuff.
- 53:32I just, I know I'm going to be doing some Excel stuff.
- 53:34So let's do the Excel writer one.
- 53:36And then, you know, maybe later it's like, oh, I know I'm doing survey type of data.
- 53:40So let me work with explode and just try to, you know, if you work these in one at a time,
- 53:45eventually they become part of your tool chest and they're good, right?
- 53:48Yeah.
- 53:48Yeah.
- 53:48And just expanding your tool set and skills.
- 53:51I think part of the trick is to make sure that you apply it a little bit, right?
- 53:55I mean, you know, they're out there, but just as you use them, like bring them in.
- 53:58It just saves you time and resources.
- 54:00Awesome.
- 54:00Yeah.
- 54:01Half the battle is just knowing that it exists, right?
- 54:03It's not that it's necessarily hard to use.
- 54:04It's like, I just didn't know this was even an option.
- 54:07Yeah.
- 54:08All of these are very easy to use.
- 54:09You just know that they exist.
- 54:11Yeah.
- 54:11I feel like so much of Pandas is that way, but they're so, it's hard to know because there's
- 54:15so much to do there.
- 54:16It's cool.
- 54:17Out of the live stream, Brandon, just wanted, now we're cutting it out.
- 54:19I wanted to throw out, he said, very helpful.
- 54:21Thank you for the article, Bex.
- 54:22Cool.
- 54:23You're welcome.
- 54:23Yeah, I agree.
- 54:24Yeah.
- 54:24Thanks for doing this one.
- 54:25I do want to point out, we certainly don't have time to cover it, but let me pull it up
- 54:30here so I can make sure it goes in the links as well.
- 54:32You did the same thing for NumPy, right?
- 54:34And you also were a little more confident.
- 54:36I got to say, you're a little more confident here.
- 54:37Your P of guarantee equals 0.85 instead of 0.8.
- 54:40NumPy practices are a little bit harder to understand.
- 54:44That's why most of them don't bother to learn those, most people.
- 54:48So I was a bit confident because I also didn't know most of these functions.
- 54:52That's why I was a bit more confident.
- 54:54Yeah.
- 54:54Fantastic.
- 54:55All right.
- 54:56So if people like this flow and they want to kind of go a little deeper and go into the
- 55:00NumPy layer, they can check that out.
- 55:01And they can also check out a bunch of your other writing.
- 55:03I also have the same for SK Learn.
- 55:05Okay.
- 55:05Right on for SK Learn.
- 55:07Great.
- 55:07All right.
- 55:08Anything else you want to add to this article before we call it good on that topic?
- 55:12I think we covered everything.
- 55:13Yeah.
- 55:14We covered it well.
- 55:14I think it was fun.
- 55:15Yeah.
- 55:15It was fun.
- 55:16All right.
- 55:16Now, before you get out of here, there's the two questions you've got to answer.
- 55:21If you're going to write some Python code, what editor do you use?
- 55:25What are you going to use?
- 55:25For data analysis, I usually use JupyterLab.
- 55:28Yep.
- 55:29But if I have to do pure Python, that's always PyCharm.
- 55:33I love it.
- 55:34Awesome.
- 55:34That's a good combo.
- 55:35Yeah.
- 55:35And then notable PyPI package.
- 55:38Something.
- 55:39It doesn't have to be something super popular, but something that you've been across that
- 55:42people are like, you're like, people should know about this.
- 55:44This is something I learned about.
- 55:45I recently come across with UMAP.
- 55:48UMAP?
- 55:49It's for dimensional add to reduction.
- 55:50UMAP Python.
- 55:51It's usually used for like very large data sets to project them to 2D so that you can
- 55:57visualize them.
- 55:58This one is a really useful package.
- 56:00Nice.
- 56:01So definitely people are trying to project down to 2D.
- 56:04I mean, that's one of the problems, right?
- 56:05Is how do you look at some of this stuff that's...
- 56:08Like 100 dimensional or 200 dimensions.
- 56:10You just can't visualize.
- 56:12I don't even have any idea at all how to do 100 dimensions.
- 56:16I remember we were doing some work with complex analysis and two dimensional.
- 56:21Each dimension was complex numbers.
- 56:23So four dimensional.
- 56:24That was a challenge.
- 56:25I have no idea how to approach 100.
- 56:27No one does.
- 56:28That's why this kind of dimensional add to reduction techniques exist.
- 56:31Yeah.
- 56:31Fantastic.
- 56:32And of course, important machine learning and stuff, right?
- 56:35There's like dimensions that you can just throw away because they don't actually contribute
- 56:38to the predictions and stuff, right?
- 56:40Yeah.
- 56:40You might does that exactly.
- 56:41Excellent.
- 56:41Super.
- 56:42All right, Bex.
- 56:43Thank you for being here.
- 56:44Final call to action.
- 56:45People want to get deeper in Pandas, maybe learn more about some of your articles.
- 56:50You know, what do you tell them?
- 56:51As I said, just first check the documentation.
- 56:53The documentation is usually, it should be your first choice.
- 56:56It's the best place to learn about a library.
- 56:59It takes a little dedication, but go through it and find out what it has to offer and go
- 57:02from there, right?
- 57:03It's a bit hard to read, but the documentation is always like gives the best information about
- 57:09the library because it's written by the package creators.
- 57:13So they know the library the best.
- 57:15For sure.
- 57:15Yeah.
- 57:16All right.
- 57:17Well, thank you for being here.
- 57:18Thanks for writing the article and sharing that with us.
- 57:19Thanks for having me.
- 57:20Yeah, you bet.
- 57:21Bye.
- 57:21Thank you.
- 57:22Bye.
- 57:22This has been another episode of Talk Python To Me.
- 57:26Thank you to our sponsors.
- 57:28Be sure to check out what they're offering.
- 57:29It really helps support the show.
- 57:31Choose Shortcut, formerly Clubhouse.io, for tracking all of your project's work because
- 57:36you shouldn't have to project manage your project management.
- 57:39Visit talkpython.fm/shortcut.
- 57:43Simplify your infrastructure and cut your cloud bills in half with Linode's Linux virtual
- 57:46machines.
- 57:47Develop, deploy, and scale your modern applications faster and easier.
- 57:50Visit talkpython.fm/linode and click the create free account button to get started.
- 57:55Do you need a great automatic speech to text API?
- 57:59Get human level accuracy in just a few lines of code.
- 58:01Visit talkpython.fm/assembly AI.
- 58:04Want to level up your Python?
- 58:06We have one of the largest catalogs of Python video courses over at Talk Python.
- 58:11Our content ranges from true beginners to deeply advanced topics like memory and async.
- 58:15And best of all, there's not a subscription in sight.
- 58:18Check it out for yourself at training.talkpython.fm.
- 58:21Be sure to subscribe to the show.
- 58:23Open your favorite podcast app and search for Python.
- 58:26We should be right at the top.
- 58:27You can also find the iTunes feed at /itunes, the Google Play feed at /play, and the
- 58:33direct RSS feed at /rss on talkpython.fm.
- 58:36We're live streaming most of our recordings these days.
- 58:40If you want to be part of the show and have your comments featured on the air, be sure to
- 58:44subscribe to our YouTube channel at talkpython.fm/youtube.
- 58:48This is your host, Michael Kennedy.
- 58:49Thanks so much for listening.
- 58:51I really appreciate it.
- 58:52Now get out there and write some Python code.
- 58:54Bye.
- 58:54Bye.
- 58:55Bye.
- 58:56Bye.
- 58:57Bye.
- 58:58Bye.
- 58:59Bye.
- 59:00Bye.
- 59:01Bye.
- 59:02Bye.
- 59:03Bye.
- 59:04Bye.
- 59:05Bye.
- 59:06Bye.
- 59:07Bye.
- 59:08Bye.
- 59:09Bye.
- 59:10Bye.
- 59:11Bye.
- 59:11Bye.
- 59:11Bye.
- 59:11Bye.
- 59:12Thank you.
- 59:14Thank you.