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