If you want to get better at something, oftentimes the path is pretty clear.
If you want to get better at swimming, you go to the pool and practice your strokes and put in time doing the laps.
Want to get better at mountain biking? Hit the trails and work on drills focusing on different aspects of riding.
You can do the same for programming.
Reuven Lerner is back on the podcast to talk about his book, Pandas Workout.
We dive into strategies for learning pandas in Python, as well as some of his workout exercises.
This is Talk Python to Me, episode 471, recorded July 7th, 2024.
Are you ready for your host? Here he is.
You're listening to Michael Kennedy on Talk Python to Me.
Live from Portland, Oregon, and this segment was made with Python.
Welcome to Talk Python to Me, a weekly podcast on Python.
This is your host, Michael Kennedy.
Follow me on Mastodon, where I'm @mkennedy, and follow the podcast using @talkpython.
Talk Python.com.
Both accounts over at Fostadon.org.
And keep up with the show and listen to over nine years of episodes at talkpython.fm.
If you want to be part of our live episodes, you can find the live streams over on YouTube.
Subscribe to our YouTube channel over at talkpython.fm/youtube and get notified about upcoming shows.
This episode is brought to you by Sentry.
Don't let those errors go unnoticed.
Use Sentry like we do here at Talk Python.
Sign up at talkpython.fm/sentry.
And it's also brought to you by Scalable Path.
If you're a founder or engineering leader, you know how hard it is to find top-tier developers while keeping costs low.
Scalable Path is a software staffing company that helps you build remote dev teams that just fit.
Build your team at talkpython.fm/Scalable Path.
Before we jump into the interview, I want to let you know that we still have some spots left in my Code in a Castle event.
If you're looking to learn some of the premier frameworks and techniques in Python,
and you'd like to have a bucket list type of experience while doing so,
then check out talkpython.fm/Castle.
In October, I'll be running a six-day Python course for an intimate audience in a villa in Tuscany.
Half the time we'll be learning Python, and the other half will be exploring the best of what Italy has to offer.
Check out the course outline, the excursions, and all the details at talkpython.fm/Castle.
Or if you'd like to just shoot me an email, Michael at talkpython.fm, or find me on the socials, and I'm happy to talk about it.
Hope to see you there.
Reuven, welcome back to Talk Python to Me.
How are you doing?
I'm doing great.
Great to be back here.
Nice to see you.
Yeah, it's great to see you as well.
I'm a little concerned, though.
There's some possibilities that maybe my Facebook ads are going to get messed up.
We are talking about pandas and pandas internationally, and I heard that you're some kind of animal trafficker.
Do you want to start the show with that story?
It's out of control.
It is the craziest story.
So I occasionally advertise on Facebook, advertise my pandas and Python training.
And I guess it was like two years ago, I tried doing a little bit more advertising.
And basically, I didn't really pay much attention to it until about a year ago when I noticed that when I tried to do some more advertising and it said, you are not allowed to advertise on any meta properties.
That's really weird.
Like, what did I do?
I looked.
I could not find any indication of what I'd done wrong.
So it says, if you want to appeal, click here.
So I clicked here.
And within 30 minutes or so, I get email back saying, your appeal has been checked and denied.
You will never be allowed to advertise on any meta property again.
And this was like, what have I done?
Like, what are you teaching?
I'm pretty innocent.
Some courses and some books.
Come on, man.
I figured also, I appealed.
Someone must have looked through this.
Anyway, in poking around online, someone said to me, oh, I was caught by the same thing.
It is illegal to sell rare and endangered animals.
And they believe that since you were selling Python and Pandas training, then you must have been training in live and endangered animals.
And thus, you are banned for life.
Well, I said, you know what?
I got some, first of all, like nuts.
And yet, a great story to tell to my machine learning classes.
So good on that front.
But I was like, this is easy to resolve, right?
I'll just contact someone at Facebook.
No one's available.
I got to text on my connections, like people I know who work there.
And I finally get the answer back saying that for legal purposes, they delete all data that's more than six months old.
Something like that.
So they could not go back and check to see why I was banned.
And thus, this ban really would be for life.
So because I waited a while, because it had been like a year between the ban and me noticing.
Oh, my gosh.
So they couldn't do anything about it.
So I was like half laughing and half like, you got to be kidding me about this.
And I posted on my blog about this.
And you guys picked it up on Python Bytes.
It was picked up in a bunch of other places like Hacker News.
And about a month later, I checked and it was back.
No one told me anything.
No one said anything.
Just magically and quietly, my account was restored.
Running to the press never helps.
It never helps.
That's right.
But it was so absurd.
And I think it was at least like four senior engineers at Facebook who tried to help me with this.
And they were all like, we tried.
There's nothing we can do.
Crazy.
It reminds me, honestly, it reminds me so much of my App Store review experience, getting the new Talk Python training mobile apps in iOS, in the apps, in the iOS App Store, in the Google App Store.
They were tragically incompetent in their own special way, right?
Somewhat malicious, somewhat not malicious.
Just like, for example, Apple.
I think it was Apple.
It could have been.
I don't know.
Apple or Google.
It has to be one of them.
Said, you know, we've denied your application to publish this application because you're trying to impersonate in an existing one.
I said, what app am I trying to impersonate?
Like, it's hundreds of hours of stuff that we've created.
Like, they're like, well, first you might be hijacked.
So, well, in the description, it says, if you want to learn Python, you can take our courses.
Well, there's already an app called LearnPython, and you're trying to impersonate.
I'm like, what?
Like, it said if you want to learn Python, but there's an app called LearnPython.
I'm like, I just, I don't even under, like, my mind is, how do you read that description and think this is a trademark?
So, if you come up with an app called, like, Eat at a Restaurant, then no food order delivery place.
Yeah, you're going to be acquired by Uber Eats straight away.
Exactly.
And so, we went, but we, you would think, okay, fair mistake.
Like, yeah, yeah, okay.
Something caught it.
Just like you said, a simple request and a human will look and it'll be fine.
No, they're like, well, look, obviously you're doing this.
I'm like, obviously.
Let's try another scenario.
What if I said I wanted to learn to play the guitar?
And there was an app called Learn Guitar, but I don't want to learn.
It's not, it's not, the title is not Learn Guitar.
It's just the act of learning a guitar.
How else would you explain it?
Like, oh, okay.
So, I guess we see, we understand now.
You can have that sentence.
And it's just, these things are really, and you're at the complete mercy of them.
It's really, it's both like comically funny, but it's also like painful because we spent
four months building that app.
They wouldn't accept for this stupid reason, you know?
And they're taking a fair amount of money off the top for whatever people are earning from
these apps.
It's not like this is a charity or something.
What I found amazing with Facebook was there was literally no way to contact a human being.
Like, I tried all sorts of searches and forums and on, on, and on.
And like, and, and I found a lot of other people who had been caught up in this sort
of ridiculous situation, but like, there's no form even to say, hey, I think you made a
mistake.
Why don't you have a human look at this?
Because that would cost them too much, you know, someone's salary.
Yeah, absolutely.
It's, anyway, I don't want to spend too much time on it, but boy, is that a crazy story?
I mean, we're going to be trading in some, we'll be trading in some pandas today.
And so I just, I'm going to bleep that part.
Every time when the word pandas is said, we're bleeping it out on the YouTube.
version.
Well, this could be entertaining then.
Wow.
Ruben was really testy.
Like all those bleep, bleep, bleep.
No, seriously though.
You know, let's, let's catch up.
We'll talk about your book.
What have you been up to since the last time we spoke?
I can't remember the last time I had you on.
It's been a couple of years, I think.
It's been, it's been some time.
Yeah.
So I'm continuing to do Python pandas training at companies.
Since the pandemic, much more of it is online, just because companies are now used to doing
stuff on Zoom, team, WebEx, rather than bringing me there in person.
That's fine.
So I traveled to conferences rather than to clients.
And, you know, it sort of extended my flexibility on that.
And I've also been building up my online training stuff, both a whole lot of courses.
And I've got an online bootcamp that I run for Python and pandas twice a year.
The big thing, which is actually connected to what we're doing here also, is I have a new
newsletter called Bamboo Weekly, where I have pandas exercises.
It's sort of like the same style as the book, but every week I take a topic and I do it, I
do it based on current events.
So if there's something going on in the news, I try to find a data set, a real world data set
that has something to do with that.
And then we try to experiment with that.
Yeah, that sounds really fun to see like a real world example every week.
Yeah, yeah.
So people are like, how do you come up with that?
And I say, well, I listen to a lot of podcasts and I read a lot of newspapers and something
somewhere, like I read The Economist, for example, and they had this short, cute article about
the number of animals that go through Heathrow Airport every year.
I was like, wait, there's got to be a data set for that.
And sure enough, the Heathrow Airport Authority publishes a data set in CSV of how many animals
go through.
And it was like 200 horses and one and a half billion butterflies and everything in
between.
And so it was great fun to sort of play with that data and ask questions about it and give
people practice with something that's dirty and messy.
I don't mean the animals being dirty, right?
Like very messy.
Like you need to like really, you know, wrestle with it because that's the only way you're
going to improve.
So I'm having a lot of fun on all the training front.
I definitely see more and more use of, I'm sure this won't surprise you, more and more use
of Python in the data space as it just like catches fire there.
Yeah.
It's just going downhill, picking up speed, isn't it?
It's extraordinary.
Like I still remember asking people in my intro Python courses, so what are you here for?
They're like, yeah, my company's thinking of doing some stuff with data analysis and pandas
or at least as data analysis, I knew that even in NumPy before pandas got you out.
And me thinking, hmm, I should really learn this stuff because it sounds like it's going
to be popular.
And holy cow, it's like it's everywhere.
And the whole ecosystem is just growing, growing, growing.
It's like people are sort of seeing pandas as the like underlying infrastructure on which
they build their software tools and their companies.
Or even the thing that defines the API on which they can innovate, right?
Think DASK or a whole bunch of other things in that space, right?
That if you know pandas, chances are, if you're not too crazy, you can like do grid computing
by changing the import.
It's extraordinary.
Just extraordinary.
Yeah.
Yeah, it absolutely is.
And so back to your newsletter, pandas eat bamboo.
Is that right?
That's so-
I understand why you're banned from Facebook.
Okay.
All right.
It was actually my father's idea.
I was like, I need a like a catchy name for something.
And so he was like, well, what was something with bamboo?
I was like, right, right.
It's like, you know, it's food for thought and food for pandas.
Yeah.
That's actually really clever.
I like it a lot.
So you've been on a journey.
You've been on a journey to write a book, right?
Yes.
I was convinced to do a second one.
Right.
So I did a book with Manning called Python Workout, which was exercise in Python.
And when that finished, they said, so what other topics do you think would be useful?
I said, well, I'm doing a lot of pandas and people definitely need a lot of exercise and
a lot of practice in that.
And so I got to work both collecting the exercises I do with my corporate training and also like
coming up with some new ones as I learn new things.
Because pandas is so, so, so huge that it's very easy to get lost in there and not even
know what are the important topics to learn.
And so thanks to like working with people all the time, I sort of see also where they
get stuck and where they have problems and where it's really confusing to them.
So yeah.
So 200 and some odd exercises later.
By the way, I'll just tell you why did it come out when it came out?
Because I'm terribly bad at deadlines.
I said to the Manning people, I really want to have it at my booth at PyCon.
They said, okay, big talker.
You want it mid-May, huh?
So you better have everything done.
And they sort of backtracked on the calendar and said, okay, so you better have the whole
thing done by December.
And then finally I had some like, you know, fire under me and got it done.
Awesome.
As opposed to, I should get more done.
Yeah.
The version I actually have in my Apple Books here I pulled up is the Manning Early Access
Preview version.
But it's out for real now.
Yeah.
Here.
I've even got the paper copy here.
Woohoo.
Look at that.
I know.
That's a pretty hefty book, honestly.
I still keep looking at it.
That's a proper book.
It's like, wow.
I know.
It feels like it's really quite some feeling there to see.
I guess I really finished it.
Congratulations.
So I wanted to talk to you about maybe just kind of falling on with your bamboo idea.
Like give us some examples.
Give us some problems that people are solving with pandas.
And I mean, we're not going to talk through the code super detailed, but you could say like this
aspect or this feature of pandas like dot look or whatever is how you access and solve these
problems, right?
Like, so just kind of exploring that space.
I had Wes McKinney on before five episodes ago, something like that.
And I was just like, how do you learn pandas?
It's like so big, you know?
So I've actually changed.
Well, let me first say I'm definitely one of those people who thinks you should learn Python
before pandas.
Like I definitely think that knowing the language well will serve you very, very well in all sorts
of weird, small ways necessarily.
But at the same time, when you learn pandas, you have to learn that some of the paradigms
you learned, like some of the idioms from Python are not appropriate.
So I was giving a class in like optimizing pandas, like a short class, we'll call it micro
class, like 90 minutes long, about a year or so ago.
And at the end, I was like, oh, and by the way, obviously just never do for loops.
And everyone's like, wait, wait, wait, what?
I said, what do you mean what?
And they were like, we were taught in our intro pandas class that you should do a for loop
to do anything across the data frame.
And I was like, okay, what?
How can this be?
And so people think that because it's in Python, you should do it the same way.
But there are all these different idioms, especially whole vectorization that you need to internalize.
Otherwise, as I like to say to people, you should hope to be paid by the hour because like these
things are just going to take forever to run.
And so people don't necessarily understand sort of how to approach pandas stuff.
And then they don't understand sort of, let's see, I mean, I'll give like outline and then
we can sort of go into it.
Certainly how to access things with .lock.
Certainly how multi-indexes work.
How to work with the different d types.
Because these are things that we don't think about in the standard pandas world each day.
The closest analogy would maybe be that where you're working with a dictionary, you want
to think about what your keys are going to be.
But even then, it doesn't come close to it.
So like .lock, people are like, so fine.
So .lock is used for retrieving from a series or retrieving from a data frame.
But it's so much more than that because you have two parts to it, or potentially two parts.
You have the row selector and you have the column selector.
And each of those can be an individual name, a list of names, a Boolean series, or even a
Lambda.
And you can mix and match those in so many different ways.
And once you see those options, like your head sort of explodes with, oh, wait, I never thought
that I could access my data like that.
And then you're like, wait, you can also assign your data like that.
And then they're just sort of astonished.
Yeah, it's really wild.
You know, I think pandas has its own idiomatic style that is different than what you would
call pythonic, right?
Like it's pandonic.
I don't know what the name is, but idiomatic pandas, right?
I like that.
Where there's things that are specific to pandas, like this vectorization stuff, right?
Instead of looping over, right?
You know, you think about the Python performance angles or the data science performance angles,
right?
A lot of the speed that we get out of tools like pandas and numpy and polars and others is,
as you take the data, you push it down into some native layer and you just leave it there.
And you kind of speak to the native layer from Python and you say, deep down in your insides,
you got this thing, multiply all 1 million of them by two or whatever, right?
But if you loop over it, you're like converting it out of C into Python objects, then you're
operating it, then you're putting it back just 2 million times.
All of a sudden, all those benefits are gone.
And so certainly learning those types of things.
I mean, the vectorization, I think most people get pretty soon, although it sounds like not
that's really everyone.
I was really like flabbergasted.
But there's way more than that, right?
There's a whole, that's just probably the most obvious thing.
This portion of Talk Python to me is brought to you by Sentry.
Code breaks.
It's a fact of life.
With Sentry, you can fix it faster.
As I've told you all before, we use Sentry on many of our apps and APIs here at Talk Python.
I recently used Sentry to help me track down one of the weirdest bugs I've run into in a long
time.
Here's what happened.
When signing up for our mailing list, it would crash under a non-common execution pass, like
situations where someone was already subscribed or entered an invalid email address or something
like this.
The bizarre part was that our logging of that unusual condition itself was crashing.
How is it possible for our log to crash?
It's basically a glorified print statement.
Well, Sentry to the rescue.
I'm looking at the crash report right now, and I see way more information than you'd expect
to find in any log statement.
And because it's production, debuggers are out of the question.
I see the traceback of course, but also the browser version, client OS, server OS, server OS version,
whether it's production or Q and A, the email and name of the person signing up.
That's the person who actually experienced the crash.
Dictionaries of data on the call stack and so much more.
What was the problem?
I initialized the logger with the string info for the level rather than the enumeration dot info,
which was an integer based enum.
So the logging statement would crash saying that I could not use less than or equal to between strings and ints.
Crazy town.
But with Sentry, I captured it, fixed it, and I even helped the user who experienced that crash.
Don't fly blind.
Fix code faster with Sentry.
Create your Sentry account now at talkpython.fm/sentry.
And if you sign up with the code TALKPYTHON, all capital, no spaces, it's good for two free months of Sentry's business plan,
which will give you up to 20 times as many monthly events as well as other features.
Accessing things in that way.
By the way, you asked before, like, how should people even approach learning pandas?
And so I've started thinking about it a little differently based on feedback from people that I would sort of walk through it.
Okay, here's a series.
Here's a data frame.
And after creating some, you know, fake ones with, you know, made up numbers or random numbers,
then we'll start reading in files.
And then we'll do this.
And then we'll get to visualization.
And a student of mine said, you know, you're sort of missing the big, like, people miss the big picture doing that.
And they want to get the excitement.
Why don't you start with read a CSV file, visualize it right there inside of Jupyter,
and then people will be so impressed and amazed.
And then you fill in the gaps.
And so I've started doing that a bit.
And I think that has not been a bad approach to catch their attention, give them a sense of what the possibilities are.
And like, okay, let's now walk through each of these little pieces and build up to what we saw that first day.
And that's been fun.
Yeah, I totally agree.
That's also something that I really strive for.
I don't always do whenever I'm doing presentations.
But, you know, just because someone is chosen to sit in a seat for a day-long course or an hour-long presentation doesn't mean that they couldn't use a little inspiration, right?
And if you're like, wow, you did those three lines.
And now we have this picture.
And I understand all that.
Like, tell me more.
Right?
That right there.
That's right.
Right.
Now you have their actual attention the whole time.
And they're enjoying it.
And it's, yeah, it's so often it's like, well, in order to show you the nice stuff, I got to give you every level of detail.
It's like, no, you don't.
You're going to make a leap.
Don't do it that way.
That's right.
And you mentioned, like, three lines of code.
One of the amazing things about Pandas is how often you can write very, very little code.
But it's, like, getting to that code that takes a while, really taking advantage of it.
Yeah.
We've been knowing that it's possible, right?
Right.
Right, right, right.
People don't even know.
So a lot of times, like, I've been using it now for long enough that I sort of intuit, oh, there's got to be a method that does this.
Like, someone has encountered this problem before.
And so either there's a method or there's an option or there's an add-on, like, something somewhere that just makes it trivially easy.
And that's part of the exploration that I try to do both in the book and the weekly and, like, in my training general.
And also, truth be told, I'm constantly learning stuff, right?
Like, it's rare for me to teach and not discover some option or some method that I did not know about because it's just so incredibly vast.
Yeah.
Or Pandas 2 comes out or something like that.
Yes.
Yes, indeed.
I mean, I've been exploring, I mean, tomorrow, tomorrow I head off to Prague for EuroPython,
where I'm giving a talk on PyArrow in Pandas.
And so I've been looking into that a lot.
And, oh, boy, right?
I mean, I've been using it for, say, a year or so.
But it's amazing.
And yet there are all these subtle changes that are happening in Pandas as a result that you need to know about or you need to expect when you use it.
But it's another, like, tool in our toolbox that we can pull out to make Pandas more effective, more efficient, deal with larger data, and also interact with other and interoperate with other systems.
Yeah, for sure.
It's just like, as I said, the whole ecosystem is just exploding.
It's really quite something.
Yeah, it really is.
And I think the Pandas 2 stuff is going to make a pretty big difference, changing, like, the internals away from just tables of numbers, basically.
Okay.
Let's talk about, so the way I thought we could maybe explore this Pandas workout book is let's just pick some fun exercises that you put together and talk about them.
Give us a quick overview of what the workout aspect of this book means anyway.
Sure.
So the idea is that you can't learn everything all at once or quickly.
That's sort of like working out physically.
It's a long haul.
And every day you get a little better, a little stronger, a little more flexible.
And so if you see, you know, your Pandas learning journey as it's going to take me several months, not going to take me a day.
Then if every day you do a little bit of practice, you learn something new in some new direction, at the end of that journey, you're going to be able to solve many, many more problems in better, more idiomatic and more efficient ways.
And you'll be able to put these pieces together in ways that you didn't even expect.
So that's the basic idea.
And so the book is divided into, I want to say, 12 chapters.
I know we sort of rejiggered it at some point where each chapter focuses on a different aspect of Pandas.
But it's really sort of the total experience of going through it.
And so we have 200 exercises, plus there's like a, for lack of a better term, a midterm and a final.
Like a meteor projects that people can go through that people ask for after the Python book.
And each exercise then has not only the main exercise where I pose a problem, I give an explanation, I give an answer, note the order.
The answer comes at the end, so you won't peek as easily.
We talked about putting it in the back of the book and like that just didn't work out so well.
So at least you have to wade through the explanation a little bit or turn the page.
And then after the-
You can buy the one book with the questions.
You know, there was talk about that too.
Oh, you're serious.
Okay.
You can buy the answers later.
There was definitely-
I was joking.
Ten times the price.
I didn't mean to give anyone ideas.
I'm joking.
No, no, it's fine.
And then like after the answer, we have three, what we call beyond the exercise, which are,
okay, now that you've kind of gotten the basics, let's push either on the same data set or even like sort of go farther.
And so it's like, that's why we say it's 200 exercises because it's 50 official exercises and other three for each one.
And those tend to be much harder.
And I don't give a full explanation, although I do give the solution online in a Jupyter notebook.
You can download Jupyter for all these things and see how I solved it.
Nice.
And for many of them, there's a link to the Pandas Tutor.
I think, yeah, you've definitely spoken to Philip well in the past.
So he is not only a Python Tutor, but Pandas Tutor.
He and his team.
It's amazing.
And so you can just click on a link and it will take you to usually a miniature version of the data set because it's too big for Pandas Tutor.
And then you can sort of see the visualization, how these things work.
Yeah.
I don't think I talked about Pandas Tutor.
Maybe I have talked to Philip about it, but I've certainly used it in some of my courses.
But it's worth bringing it up just to talk.
Like, see how that thing works?
It's something special, this thing.
It really gives you some deep understanding into like, okay, if I run, you know, this group by command, then here's how all the pieces like flow back together.
And so you're using this during your book?
So, yeah.
So, I mean, every exercise has a link in the solution.
You click on it and it brings you to the code that I use.
It's usually, again, a sort of miniature version of the code, miniature version of the data set that you can then see the visualization there in your browser.
Nice.
Yeah, because you've got to basically, I think, encode the data in the URL.
So it can't be too much.
Right.
So I would like basically take the data in the data frame, turn it into a Python dictionary, and then like set it up because it won't work with files for security reasons.
Yeah.
So assign that dictionary to a variable or no, no, I'm sorry, I turned to CSV in a string and then assign that string to a variable and read it in.
And then I would see if it overflowed the pandas work, now pandas tutor limit.
And if it did, I sort of iterated until I got it small enough to fit in there and big enough to be useful and interesting.
That took a few iterations.
That's cool.
No, but it's a super, super good resource.
For learning pandas, I also think for exploring, right?
Like you end up with some code that like, I'm not really sure what this does, or this is actually new to me.
Like a lot of things you might encounter in this book.
You're like, let me visualize that.
Right.
Because, yeah.
Right.
I mean, as I said, like I do a lot of training in pandas and very often people have already played with it a bit, used it, even used it for a year or two.
And because it's so large, like it's not unusual for someone to say, oh, I had no idea that this functionality existed.
Why have I been wasting my time doing X and Y and Z?
I'll give you one example that I've been using more and more.
So, you know, Matt Harrison's a big fan of the method chaining approach.
And at first I was like, yeah, yeah, yeah, Matt, whatever.
Like, yo, stop pushing out on everyone.
You and your fluent interfaces.
Keep them to yourself.
Right.
And then I was like, actually, this is a great way to build things up little by little, line by line.
And I can use this.
It's pedagogically very useful.
Because I say, okay, let's think about how we want to break down this problem.
We'll do this.
Then we'll do this.
Then we'll do this and you can see it sort of going line by line until voila, we have the analysis that we want.
And so I inserted that into a lot of places in the book.
Sort of like one of the last edits that I did was to go back and change it to be more method chaining.
And I use it now all the time in my training and in Bamboo Weekly.
And so I bow to Matt on that.
He was right.
And I was stubbornly resistant for no good reason.
Yeah.
I really like that style as well.
You see, it's officially night here where I am.
It's just everything switched dark.
I just said that yesterday when I was doing office hours and all of a sudden I was sharing my screen and it changed the color.
I'm glad I'm not the only one that happens to you.
So, yeah, I'm a big fan of the method chaining fluent interfaces.
I mean, I would love to even see like Python itself in the standard library adopt that more.
Right.
Like there's so many things you operate on that will change something, but then it will return.
It will not return anything.
It's like a void method as much as we have those.
Right.
It returns none effectively.
So you can't say, you know, dot sort, dot this, dot that.
You have to like multi-step it.
And I would just love to see more of it.
But let's talk.
Well, I'll just say there on that front.
So Candace like does have the option to either get back a new data frame or to say in place equals true.
And then it does it locally, like it does it on that data structure and then returns none.
And people are consistently convinced that this is faster, more efficient, better.
And so I've been like trying to tell people, no, the Python core developers keep saying, do not think that is true.
It is not true.
And we are getting rid of in place equals true at some point.
Stop using it so that you can do method chaining.
And so no small number of people, again, in my course, are like, oh, really?
I had no idea.
I feel like, you know, I'm spreading the gospel as a word.
Throw that whole expression in some parentheses and dot yourself away.
Let's go.
All right.
That's right.
Let's dot our way on over to exercise seven of the many.
And let's just talk about long, medium and short taxi rides.
Tell us like kind of like we can only talk so much about the code, but like let's let's talk a bit about it and get people.
Like I said, I want to expose people to like what are some of the problems and aspects of pandas that you can use to solve them.
Sure.
So one of my favorite data sets to work with is New York City taxi information.
It's like everyone can identify with it.
You understand it.
And so this exercise uses a very small subset of that.
Maybe we'll talk next about the pandemic taxis, which is a much larger one.
But this is just 100,000, no, 10,000, 10,000 taxi rides from like five years ago, six years ago.
And the question is, well, how can we divide up this data set, which tells us how long, how far, how much people paid, when they were picked up, all that information.
How can we find out like the distance that they went and categorize that?
And the reason that this can be useful is we so often have numeric data that we need to put into categories, right?
What are best sellers?
What are poor sellers?
Who are, you know, the most, you know, employee of the year, that sort of thing.
There's so many places where turning something into a category will be useful.
And so it's very tempting to think, okay, I'll like do some if statements or I'll do some for statements.
But actually, pandas provides us with pd.cut, which just does it for us.
And this is one of those examples of once you learn it, you're like, oh, wow, I get it.
I don't have to have, oh, yeah, I even have here.
Like you might think you would want to say, let's set all the categories, you know, to be medium.
And then where it's less than two miles, we'll call it short.
And where it's greater than 10 miles, we'll call it long.
But you can just say pd.cut, we're going to cut it two.
We're going to cut it 10.
Anything less than two is short.
Anything greater than 10 is long.
Anything in the middle is medium.
Done.
Oh, that's interesting.
And pd.cut gives you back a new series.
This portion of Talk Python to Me is brought to you by Scalable Path.
If you're a founder or engineering leader, you know how hard it is to find top tier developers while keeping costs low.
That's where Scalable Path comes in.
They're a software staffing company that helps you build remote dev teams that just fit.
If you're wondering what sets this staffing company apart, well, one big differentiator is their approach.
They're founded and run by developers.
Scalable Path understands that finding the right developer is not just about technical skills.
It's about personality, work ethic, and how well they mesh with your team.
Their software architects will take the time to understand your vision and needs and then develop technical challenges for the roles you're looking to hire.
And these technical tests are conducted live on video by senior software developers so there's no gaming the system.
And Scalable Path takes it one step further.
They evaluate each developer's soft skills like communication, attitude, and work style before presenting best suited candidates to you.
Scalable Path has built a network of over 35,000 remote developers.
No more endless searches or sleepless nights worrying about the right hire.
And here's a special offer for Talk Python listeners.
You'll get 20% off of your first month.
So are you ready to scale your dev team and your business?
Get started by visiting talkpython.fm/scalablepath.
The link is in your podcast player's show notes.
Thank you to Scalable Path for supporting the show.
So the way you're doing it is you're setting everything to medium and then you're defining short and you're defining long instead of defining the three categories.
So that's like the sort of you might think this would be a good way to do it.
And it works, right?
As I like to say in my courses, unfortunately, this works.
Right?
Like so you will get the right answer this way.
But if you then like go to the next page here, you'll see that you can just use PD Cut.
I see.
Yeah.
And then you just say, here are the bins.
Here are the labels.
Go.
Well, this goes back to that thing we talked about, like looping over stuff versus just going, this is what I want you to do.
Do it 100% deep down inside the best you can.
Don't bother me.
Just figure it out for me.
Right?
That's exactly right.
And this means that like a whole lot of people have worked on PD Cut and have made it work efficiently, way more efficiently than you or I could do in our code.
Presumably.
Right?
It's not going to be any worse.
And it'll probably be a lot better.
The other thing is you get back and you can see it sorted at the end, the output of the series you get here.
So you feed it a series and you get back a series.
And the series looks at first glance like it's a bunch of strings.
And so you're going to have short, short, medium, medium, long, long, long, but it's actually not strings.
It's actually a category, which is a pandas version of an enum.
So really it's very small because it's just integers being stored there.
And then those integers are associated with strings.
So that's an example of like where they have thought through it.
And they've basically said, yeah, we're going to make this more efficient than you would probably think to do on your own.
Right.
Awesome.
Well, I would, you know, to be honest, I've been happy if I came up with that first solution.
Just using lock instead of there.
That's pretty cool.
But this, this cuts is super nice.
Yeah.
Just to find the boundaries with as bins and then off it goes.
Right.
And there's an option there include lowest.
And so I actually didn't know about this for a while.
So I'd be like, okay, so, so think about the bins has to be from some, some, you know, some small number to some large number.
And the question is then, well, wait a second.
What about that leftmost bin?
What about that?
Like if it's up to and not including, it's like less than, but not less than equal, then how do I do it?
So I would always be like, well, I'll take the min.
I'll do like the series dot min minus one.
And then it's guaranteed to be lower than that.
But no, it turns out the pandas developers thought about this long before I did.
And there's an option, a keyword argument you can pass, include lowest equals true.
Done.
Now it's less than an equal as opposed to just less than.
Right.
Because all these kind of things, the boundary conditions are always tricky, especially on floating points, right?
Oh, yes.
There's probably at least two spacecrafts that have crashed because of this.
All right.
The next one we want to go to is number 12, Finding Outliers.
Yeah.
So, you know, it's this is not a book about statistics and I'm not an expert in statistics, but there are a whole lot of statistical ideas that permeate working with pandas.
And so it's like, you know, mean, standard deviation, median, understanding sort of how they can differ from one another.
Right.
And especially the whole mean versus median thing where it's so easy to have a few outliers pull your data up or pull your data down and then sort of fool you.
Right.
And so often people want to find the opposite.
And I've certainly found this, especially with corporate training with like cybersecurity people where they're like, we are always looking for outliers.
Like, you know, who was the user who was logging in at unusual times?
Right.
Who logged in for 20 IP addresses within one minute.
Right.
That's right.
That's not a good person.
They're not good.
I'm to tell you just that right now.
They just try to be very, very effective.
And so and so.
So this finding outliers is, OK, let's find out then not who was sort of normal, but like who was abnormal.
Right.
Who who is exhibiting, you know, unusual behavior.
And so here it's like, OK, let's take our set of numbers and let's find out who was more than one standard deviation.
I can't remember if it's one or two above the mean or below the mean.
And let's just find those values and ignore the sort of normal values.
And then we could talk a little about, you know, IQR, the inter-portile range, which is a standard statistical idea that we don't talk about enough, even though it's super useful.
Right.
People came up with these ideas a long time ago.
Oh, here's a fun fact.
John Tukey, who came up with a lot of these ideas, he also invented the word bit in computers.
Oh, wow.
OK.
So, yeah, I was like, wow, what a guy.
So you can see here then that like I'm going to take the, you know, trip distance.
Let's find, you know, trip distance.
Let's find where the trip distance is less than the trip distance at the first quarter of the first quarter minus one half times the IQR.
Right.
Meaning let's take the distance between the first quarter and the third quarter.
That's our inter-portile range.
That gives us a sense of like where is the bulk, where are the bulk of the numbers?
And let's find out who is below that or who is above that.
And so it's not surprising if we're looking for taxi distances, you're not going to have a lot of outliers that are very low because you can't go below zero miles in your taxi.
But you can go very high.
You can go very large.
And so looking for those trips that are greater than the 75th percentile plus one and a half times the IQR, we actually find, you know, I see here in front of me, you know, about 1800 taxi rides out of the 10,000.
So about 19 percent of the taxi rides are much, much longer than the mean ride.
Interesting.
And you can use this and you can imagine people in like the New York City Taxi Limousine Commission saying, oh, we can use this to plan, to charge, to, you know, send taxis to different places.
Or have a special program for long distance stuff or whatever.
Yeah.
Right.
Yeah.
Right.
Or if you're Uber, you know where to play.
They actually used to have the longitude and latitude of where people were picked up and dropped off.
And they got rid of that.
And I'm sure both for privacy reasons and because Uber and Lyft can look at that data and say, oh, well, we know now when to send cars where.
Although while true, I imagine that that data has been downloaded and archived with geolocation for certain people.
You can still get all that old data.
It's just the newer stuff that they don't do.
And they still do it by neighborhood.
I see.
I also wonder, like, how precise that longitude and latitude was.
Like, you could probably identify which home, like, was going to which other home.
Yeah.
Now you're getting into a problem.
How bad that would look.
Yeah.
Now you're getting into a shady spot or certain types of establishments, you know, that could be fraught with consequences for the people whose home you've already identified where they were picked up at or all sorts of stuff.
Exactly.
All right.
Fair.
Exactly.
Fair.
That's way more important than whether Uber can lease their vehicles strategically, you know?
Right.
And they might have some data on their own, too, you know?
Nice.
Okay.
So you can use this IQR under quartile range feature to pull that out here, right?
Right.
And, you know, we can pull that out and then do simple multiplication.
Right at the end of the day, what we're doing is we're pulling out this data and then just doing very, very simple statistical analysis of it.
Just to sort of say, how many outliers do we have?
How many low outliers?
How many high outliers?
And you can see this, like, at the end of the day, it's not that complicated in terms of math.
Sometimes people are like, well, how much math do I have to know to learn this stuff?
I'm like, I can get through it.
I promise you.
Not that much.
It's not that hard.
A few basic ideas and you're basically set.
Yeah.
Very cool.
All right.
On to the next one, which is endemic taxis.
That's a different kind of taxi.
So this was actually, so the two studies we looked at so far were with my tiny little 10,000 taxi ride sample from a few years ago.
I then took taxi rides from January and July of 2019 and 2020.
So four months there.
And the question was, I think I actually only looked at July in this exercise.
Yeah.
July 2019, July 2020, comparing taxi rides.
Now, just to remind you, July 2020, not a great time for tourism in New York City or anywhere else.
And so the question was, what differences do we see between July 2019 and July 2020?
How much did it go down in terms of taxi use?
How much less did people pay?
And then my favorite part of this was, did people use cash more in 2020 or less in 2020?
And so you see, first of all, it's like a decline of something like 80% in terms of taxi rides from 2019 to 2020.
Again, not a huge surprise to anyone who lived through the pandemic and like saw what was going on there.
And so my gut feeling was, well, no one wanted to touch anyone else or touch anything that anyone else had touched.
So clearly people would have used credit cards much more.
And no, it turns out.
There's that screen, Ruben.
You got to touch the OK.
That drove me crazy during the pandemic.
It's like, I'm going to just do touch lists.
And then you like sign it.
Are you OK?
You're like, mm-hmm.
I never thought of that.
I guess in Israel, like you just, you just like, you know, do the tapping of your car.
You didn't have to sign anything.
But yeah, the US, they're like, oh, hit OK to confirm.
It's like, OK, I thought we had just escaped me touching this disease-ridden thing.
But no.
Also, you have to like indicate how much of a tip you want to leave.
But that's a whole separate thing.
We're at the, I don't know, the car wash.
Don't you want to tip the car?
No, I don't want to tip the car wash.
That's the other thing is you should have looked at tips.
Like that could have been interesting, actually, as well.
I think, oh, I did.
I think, no, I think I did.
I definitely looked at tips, but I can't remember what it was like.
Around the pandemic, yeah.
Because I know a lot of people gave tips to like kind of say thanks.
And I was wondering if that would show up, you know, like,
thanks for being out here amongst the diseased ones, you know.
So it turns out that people used cash more during the pandemic than credit.
And I like raised this with my family.
I was like, what the heck?
I think it was my sister who said, well, everyone who would use,
like the people who are sort of higher income earners who use their credit cards more,
they were staying home.
It's the people who like were forced to go to work who use cash more,
who were taking the taxis because they didn't want to take the subway.
Like there was like sort of an in-between sort of thing.
OK, interesting.
You had all these like wild pieces of data and analysis that you can do.
And here it was not just like pulling all this data.
But one of the big things was also I want to read in a lot of data.
I want to read in from two different CSV files.
Now what do I do?
Right?
Because it's very obvious.
You read in a CSV file, you get a data frame.
You read another CSV file, you get a data frame.
But I want to treat those as one.
And I want to then be able to distinguish between them.
How do I do that?
And so sure enough, Pandas has a concat method.
And I use concat now all the time.
I read in multiple CSV files into multiple data frames.
I can cat them together.
And you can cat them either horizontally or vertically, depending on what you want to do.
And this comes back to where I said-
Their columns are the same, then you might do it vertically.
But if you want to augment it potentially, it's like, oh, now we have the sale percentage data,
but it goes along with the columns exactly or the rows exactly.
Precisely.
Okay.
Precisely.
And so another nice way to do this also is not just read this one and read that one,
but you can use a list comprehension with something like glob.
Right?
So glob.glob on star.csv.
Get back a list of data frames and then just hand that to pd.concat.
And so that's where knowing Python and be able to pull that out and use those techniques
can really, really come in handy.
Yeah.
Earlier you mentioned, I want to go back to it real quick.
You mentioned that learn Python first and then Pandas.
A lot of times when I think learn Python, I think, okay, well, learn the language and then
learn the standard library to a good degree.
And then begin to chop away at the half million things on PyPI that are interesting.
You know, and it's like a never ending sort of thing.
And then there's, of course, this joke, like I learned Python.
It was a good weekend or something like that.
Right?
Like, how do you, how do you square these two things?
But I feel like the amount of Python that you need to learn is mostly centered around the
language and really is actually not that much.
And then you kind of learn the past, the pandas way.
Right.
Theoretically, if you wanted to.
Right.
For sure.
Look, I am a big fan of objects and classes and all that stuff.
But when I'm talking to people who specifically want to use Python for data analytics with pandas,
I say to them, objects will help you, but they're not going to be crucial.
Like if there's a part of the course that you want to sort of drop, save some money, save some time, then that's a place where we can save.
Because the odds of an analyst using pandas writing their own classes are pretty slim.
I think it'll give them some perspective on how these classes work.
Sure.
But I don't think that like they need to learn that.
And like a lot of the standard library there, it's hard to say.
Right.
So as I said, I love glob.
Right.
Globbing is fantastic.
But that's definitely not in like my intro class.
I'm going to say, oh, by the way.
Yeah.
I would bet there's probably 10 modules that if you knew, you might not need to learn more for six months.
You know, doing Jupyter pandas type of work.
Right.
Like path lib and a couple of things like that.
Right.
That's right.
I mean, I just made up that number.
Yeah.
It's mostly like just being able to sort of work with the core data structures, understanding the syntax and how it works.
And even like defining some simple functions.
Right.
I think most people using pandas are not at the end going to define functions.
Although as I've gone on with my use of pandas, I see, you know, lambda, this is where it's at.
Like knowing how to use it really, really helps.
So there I do sort of go back with the pandas people.
I'm like, okay, this is going to be super weird.
We're going to talk about anonymous functions now.
And then, and then like, I typically do that with the more advanced pandas groups, not with the, the like introductory ones.
But if you can sort of wrap your mind around that, then it does help quite a bit.
It does.
So it takes what would be multiple step things and it lets you turn it into one of those changed expressions.
Because instead of defining a function somewhere else, you can just put it in line as part of a lambda and just keep on going, you know?
Great.
That's right.
And the fact that, so, so it's especially useful to find.
So we mentioned dot lock earlier.
So dot lock allows you to choose rows.
Let's start with that.
And so I can pass it a lambda and that lambda then gets the data frame you're working on as an argument.
And then whatever returns, if it returns a Boolean series, then it allows you to filter.
Well, you can then have multiple dot locks and multiple lambdas in a row to do successive filtering.
And yes, that is less efficient than doing it all in one fell swoop.
But boy, oh boy, it's easier to think about and it's easier to use.
So you just like whittle it down, whittle it down, whittle it down each line with its own lambda.
And so understanding how to do that, understanding why it's important that the lambda, inside the lambda, you're using the temporary parameter from the lambda as opposed to the overall variable for your data frame.
Because you're chaining it, that's useful as well.
Well, 100%.
And then you can also like comment out certain lines at the end and see what the intermediate values are.
And so you say it's less efficient.
It's less computationally efficient.
It might be more efficient as a human being trying to understand what the heck is happening, right?
That is spot on.
I often tell people that I think Python is a language for an age in which computers are cheap and people are expensive.
Yes.
Because like, right, our efficiency is the big bottleneck in terms of time, in terms of money.
So, right, if it takes my computer a few seconds more, who the heck cares?
Right.
My M2 Pro doesn't care if it's like a tiny bit more.
I don't care.
That's right.
Or my cluster of GPUs, take your pick.
Although at that point, it starts to cost real money to burn those things, you know?
Right.
But still, I mean, depending on that.
Look, if you see that your computation is taking a long time, okay.
So then you sort of find that and you improve that.
Yeah.
That's a really good point.
Like a lot of things are, don't worry about that until it's actually going to become a problem.
It looks slow.
It turns out it's probably a blink of an eye.
All right.
Let's keep moving on.
We're getting short on time and we have a plethora of things to work with.
I kind of want to go to wine words.
Wine words.
All right.
Let me remember what exercise that is.
That is 37.
All right.
Yeah.
So people think of pandas, not wrongly, as being great at working with numbers.
But it turns out that it's fantastic at working with two other kinds of data types.
One is strings and one is dates and time.
And you can get a ton, a ton out of analysis working with these if you know how to work with it.
So there is, I forget where it is.
There's a machine learning archive of data sets.
And one of them is 150,000 wine reviews from Wine Magazine.
Oh, wow.
And so I said, okay.
Is it Kaggle maybe or is it somewhere else?
I think Kaggle has a version of it, but I think it's elsewhere.
Wine Mag.
150,000 reviews.
Okay.
Beautiful.
I don't know.
I'll find out where it is.
Anyway.
No worries.
So I said, okay, let's find out.
Right?
Because you drink a bottle of wine and you read the back and you sort of like, you know,
roll your eyes at what they've written.
Although, although I'll put in a plug.
I read this fantastic book a few years ago called Cork Dork by this journalist who decided
to become a sommelier.
And she took the exam and her journey toward there.
She was like, and she convinced me these words actually have real meaning and people are very
serious about it.
So I will not roll my eyes quite as much anymore.
Getting hints of nutmeg, but they're troubling.
Yes.
They don't belong.
So the question was, okay, what about these reviews of wine?
What words are people using?
And are they using certain words more with California wines and certain words more with French wines
or certain words more with red or with white or rosé?
And so we can then take this text, break it apart and search for it.
We can search for it using plain old Panda stuff.
We can search for it using regular expressions, which it has built in and works very well.
So here, so let's see.
So analysis on the words.
Here, so one of the 10 most common words for red wine, right?
So how do we do that?
Well, we have to take the description and break it apart.
And if you're used to using just plain old Python, you're like, oh, well, I guess I'll
break that into a list.
But now what?
Now I have a series of lists.
Now what do I do?
And so one of the key methods to know here is something called explode.
And explode is let's take a series of lists and turn that into a very, very, very long series.
And so basically each element in the list becomes an element in the series and they all share
an index.
So you know where they're originally from.
And then, you know, the world's your oyster.
So you can get rid of punctuation, right?
So I have a S dot stir lower.
Okay.
So we lowercase everything.
Then dot stir split.
And we split into a list.
Then we say explode, get into a long series.
Then we once again run strip, angry with punctuation.
And now we can use is in, which is yet another fantastic pandas method to say, are these words
in, like find the lines where these words are located.
And then we can just do a value count.
Definitely my favorite method.
How often does this thing show up?
And then we use head.
Ta-da.
We've got the 10 most common words there.
And when you break it down like that.
The value count sorts probably most common to least common, right?
Yeah, yeah, exactly.
So value counts, not only does it count how often something shows up, but exactly.
It sorts it from most common to least common.
So then head is just head.
Yeah.
However much you take there.
And then that's the ones.
Those are the popular ones.
That's right.
So here I have like, you know, the page we're looking at.
So we get find where the country is France.
So that's the row selector.
And we want the column selector to be description.
So we only want the description.
And then we have our function top 10 words, which did what I just described.
We're going to take these common wine words, lowercase them, get rid of punctuation, so
forth, pass it in.
And we get back our top words.
And we find out what words are used to, you know, are associated with French wines as opposed
to California wines and so on and so forth.
Excellent.
Yeah.
Very, very cool example.
All right.
We, I think we got time for two more to go through.
You want to pick two that are popular or you want to leave?
Uh, let's see.
Let's see.
Let's do, maybe 32 multi-state.
Actually.
Hmm.
Yeah.
Multi.
Oh, no, let's do best tippers.
Best tippers.
Let's do best tippers.
Yeah.
You mentioned tippers.
Oh, you're going to open up a whole, this is going to be a whole thing.
Best tippers.
Number 42.
Let's go.
No, no, no judgment here, folks.
All right.
So the question is like, so we'll try to understand as everybody here when people tip their taxi
drivers more generously.
All right.
So the question was, did they tip better?
And we looked at 2019 before the pandemic in January and July.
So do they tip more in the winter or do they tip more in the summer?
And so this involves several things.
First of all, it involved using dates and time.
Again, one of these things that pandas is just amazing at that people just are not aware of all
the flexibility you have there.
Another thing is how easy it is to create a new column, right?
So we spoke before a bit how you can use broadcasting to just multiply.
I have a column multiplied by something or a column adding to another column.
But you can create a new column just by assigning to it.
Again, it's sort of like assigning to a dictionary.
It just then is there.
And so you can calculate the percentage that people tip, put that in a new column and then
say, well, let's now group by the month and let's find out.
Take the mean or the max or some sort of deviation.
Exactly.
And we can find out whether people tip more on average in January or in July.
I honestly don't remember what the answer is, which will hopefully not tick off too many
readers.
Here we go.
Let's see.
Oh, here we go.
Go back one page there.
So 32% of taxi riders in New York don't tip at all.
That's right.
That was a surprising thing to me.
And then I don't remember exactly what it was in a different month.
And you're talking summer versus winter.
There's probably a tourism angle versus non locals versus tourists.
I mean, I know people go to New York and not in the summer, but not as much, I imagine.
That's right.
There are all these different factors that, that like, you know, come into it.
Yeah.
One of the things kind of takeaways I'm feeling as we were talking about all these is like,
there's a lot of interesting questions that can be asked and answered really quickly for
like sociology and urban planning and all kinds of interesting questions that don't feel like
programmer questions.
Yes.
So, so, you know, I, I, I mean, I talked to a lot of people, you probably do too, who are
like interested in advancing their careers with Python.
And they're like, well, you know, I don't have a computer science background.
Can I got a job as a programmer?
And their vision of a programmer is either someone working at a startup or at like one
of the big companies, Google, Amazon, Facebook, and so forth.
I say to them, look, there are an awful lot of people who have great jobs working at supermarket
chains and insurance companies in the backroom analyzing data.
And they are crucial, but we don't think of them as programmers and in governments and in
cities and weather forecasting places.
Like everyone nowadays is using data and collecting and analyzing it and having these skills either
gives you abilities to do your job better or gives you the ability to move into a new job
that you couldn't have done before that is that these places are desperately looking to fill.
Yeah, I can't.
Yeah, I totally agree.
I can't remember which episode it was.
I didn't name it right.
So there's an episode I did quite a while ago about our programmers and our data scientists
and Python data scientists working together.
And I think it was the research arm of Kroger, maybe that had like 200 data scientists.
Like that's a wow, that's a proper group of data scientists.
I mean, a lot of times data scientists, I feel like there's a couple of them for a company
compared to a software team or something.
No, and I tell people also, you don't like it's worth learning to write and it's worth learning
to speak.
Not because you're going to be like, you know, a Pulitzer Prize winning writer, not because
you're going to be like, you know, whatever prize you would get for like speaking well,
just because having these skills makes you more effective at your job.
And people who can write a program to suck up some data, analyze it and come back with
a result, especially if it's like a public data set that has something to do with what
they're working on.
They are so much more valuable to their company than they would have been otherwise.
Oh yeah, absolutely.
So I found it because I have a search engine on Talk Python that searches the transcripts
and everything else.
If you want to know something people about historical shows, when you hit search, it's not just like
the show notes.
So scaling data science across Python and R episode 236 with Ethan Swan, Bradley, Bokme,
and the company is 84.51 degrees.
So anyway, that's...
Oh, I've seen that name before.
Yeah, yeah, yeah.
That's what it was.
So people can check that out.
They're interested, but...
Very, very cool.
Yeah.
Super cool.
Okay.
Last one.
What are we doing?
Let's do cities.
Cities.
What number is it?
43.
Okay.
Oh, that's right.
So I found a few years ago this JSON file containing the thousand largest cities in the United States.
So first of all, like good to work with JSON because people need to know how to work with
that kind of data.
Yeah.
Second of all, okay, let's not look at the numbers so much as let's do some plotting.
And so I'm going to like make enemies now.
I really cannot handle Matplotlib.
I find it, it's like so incredibly powerful.
And anytime I want to do even the tiniest thing with it, I have to look up the documentation
and remind myself how it works.
Maybe it's because I don't use it enough.
And so I just use the pandas interface for plotting 95% of the time.
Let's call it 90% of the time.
The rest of the time I usually use Seaborn.
So, so I said, okay, let's see if we can do some plotting here of these largest cities.
So for example, let's see growth in Pennsylvania cities, like which cities in Pennsylvania?
Oh, I'm sorry.
Like, let's see a dual bar plot.
How many large cities are in each state?
Okay.
Well, that's a group by, right?
So you have to know how to do grouping.
You have to know what you're grouping on and what, how to use count as opposed to mean,
right?
That count even exists.
But then if I want to see a bar plot where it's sorted from smallest to largest, you got
to know how to do sorting.
And then we can do a bar plot.
So sure enough, you do a dot plot dot bar on your series.
Kablam.
You have a bar plot right there.
Yeah, that's awesome.
And we just get it and like, you know, it's nicely sorted there.
Quick takeaways.
Holy cow.
Does California have more big cities than I realized?
And, you know, where's New York and New Jersey?
Like it's way down the line.
You think of those as having like pretty megatropolis type places.
Massachusetts.
That's right.
But it's how many cities.
Yeah.
Right.
So, so, so, so right.
Right.
That's like the, so keep going.
So like do a bar plot of a growth in Pennsylvania.
So how are we going to do that?
Well, wait a second.
If we want to do growth, it's a percentage, but it's written in the JSON as number and then
percent sign.
That's a string.
So we're going to have to get rid of the percent sign.
And then we're going to have to change the D type from string to a floating point.
And then we can sort it and then we can graph it.
And so the ads type changing the D type basically is a deep down inside of pandas pars as an
integer operation, right?
Rather than looping over and parsing or whatever.
Yeah.
Like basically, I mean, it has to do the looping.
It's doing the looping there at, you know, below sea level.
Yeah.
Below sea level.
Yeah.
Down by the dead sea.
I see.
Okay.
Got it.
And so you see here like this bar plot.
Now we have a whole bunch of cities that have gone down in size and a bunch of things gone
up in size.
And pandas is very happy to, you know, show, show the bar plot there.
You know, again, sorted so that we see it from the greatest sort of shrinkage to the greatest
growth.
Right.
With Pittsburgh having the most shrinkage and Allentown having the most growth.
I would not guess that.
And I would not have either given that like my sister-in-law and brother-in-law live in
Allentown and they're like, Oh, Allentown.
Actually, but I think that's a common thing that people think there, even though it seems
like a perfectly great place to be.
Yeah, sure.
Funny.
Awesome.
Well, there's a lot of cool takeaways.
One more.
Let me show you one.
Go to the last one here in this exercise.
Okay.
You got it.
So one of the most important types of plots you can do in data analysis is a scatter plot.
And you take your data frame and you say, give me the plot with based on your, you know,
the X axis should be this, the Y axis should be that.
Well, if we do a scatter plot based on longitude and latitude of the thousand largest U.S.
cities, you basically come up with a map of the U.S.
Yeah.
How interesting.
The other thing that you realize is California is technically bigger than a lot of other states.
So that also counts for Y.
Oh, yes.
Yeah.
Interesting.
But there's a lot of people in California.
Good place to be.
There are a lot of people there for sure.
If they're willing to pay the sunshine tax, it's nice there.
Cool.
All right, Reuben.
This is really, really excellent.
I know you want to give a shout out to a couple of exercises from Bamboo Weekly.
So I'll include those in the show notes as well.
And people can like take those and jump over and look at them as well.
That's fine.
That's great.
Yeah.
No, we went through a lot here just during this time.
Yeah, for sure.
And as I've discovered, there's basically, I mean, there is an infinite number of these sorts of things that you can do and different permutations of various sorts.
I mean, we didn't even talk about like, you know, multi-indexing, which opens up literally a whole new dimension of this stuff.
Yeah.
But it's great fun.
It's great, great fun.
And a lot of the Pandas 2 stuff coming on changes the foundation and opens up more possibilities still.
Absolutely.
All right.
Well, final thoughts.
Final call to action.
What do we got here?
What do you say?
Well, call to action, I mean, you can take a look at like, I guess my overall courses and so forth at LearnerPython.com.
Or if you just want to improve your Pandas knowledge, you can look at BambooWeekly.com, the newsletter there.
Does it cost money or does it just cost an email?
So it is a paid newsletter, but the first two questions and answers every week are free.
So like it's usually between five and nine questions each week.
But even if you don't pay, I still want people to get some Pandas learning improvement practice so they can totally do that.
Excellent.
All right.
Yeah.
And check out the book, obviously.
Oh, yeah.
That too.
PandasWorkout.com.
Cool.
I'll pull this up.
Yeah.
Awesome.
It's actually pretty thick.
Yes, it is.
So at the end, I was like, oh, that's why it took a long time.
Also procrastination, but you know.
Yeah.
I'd love to hear from people if they have.
I always love to hear like, you know, sort of interesting problems, data sets, issues that people are experiencing,
just so I can sort of figure out what are the next directions for me to explore that I can then try to help people with as well.
Awesome.
Well, I appreciate all your help coming on the show, sharing your knowledge, and just riffing on Pandas together.
It was a lot of fun.
Excellent.
My great pleasure.
Yeah.
Bye.
All right.
Bye-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.
Take some stress out of your life.
Get notified immediately about errors and performance issues in your web or mobile applications with Sentry.
Just visit talkpython.fm/sentry and get started for free.
And be sure to use the promo code TALKPYTHON, all one word.
This episode is brought to you by Scalable Path.
If you're a founder or engineering leader, you know how hard it is to find top-tier developers while keeping costs low.
Scalable Path is a software staffing company that helps you build remote dev teams that just fit.
Build your team at talkpython.fm/scalablepath.
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.
I'll see you next time.