Pandas is a great library that allows you to accomplish a ton of filtering and processing in condensed syntax.
But how well do you understand what's happening?
Sam Lau and Philip Guo built a great site to help us visually explore how Pandas is processing your dataset with your specific syntax.
It's called Pandas Tutor, and Sam is here to tell us all about it.
This is Talk Python to Me, episode 358, recorded February 28, 2022.
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 SignalWire and the Stack Overflow podcast.
Please check out what they're both offering during their segments.
It really helps support the show.
Transcripts for this and all of our episodes are brought to you by Assembly AI.
Do you need a great automatic speech-to-text API?
Get human-level accuracy in just a few lines of code.
Visit talkpython.fm/assemblyai.
Sam, welcome to Talk Python to Me.
It's great to be back.
It's good to have you back.
Previously, you were here with Philip Guel and your research.
Is he your advisor at UCSD?
Yeah, Philip's my advisor.
Philip is a great guy.
He's been on the show multiple times before.
And you all were here previously to talk about, you had analyzed an insane number of notebook environments, right?
Yeah, something like 60 of them.
Yeah, like 60.
So people think of JupyterLab and maybe Google CoLab.
And then is there another one?
Maybe 60.
It's insane, right?
There's just a long list.
We have like a giant table in that paper that takes up one whole page and it's a lot going on.
That was really fun to have you on to talk about that.
And I suspect that that list is longer, not shorter now.
Of course, of course.
It just is growing.
How about we just do a quick sync up?
What have you been up to?
When was that?
That was about a year, year and a half ago.
So it's been a while.
What have you been up to?
You still over at UCSD?
Yeah, so I'm still at UCSD.
UCSD being University of California, San Diego, for those who are not hanging out on the West
Coast, the U.S. and are familiar with all the UC acronyms.
Yeah, UC San Diego.
Here in sunny San Diego.
I'm doing my PhD in cognitive science.
But what that really means for me is most of my research is in a field called human-computer
interaction, which is the way I describe it is more or less the study of user interfaces,
how people interact with computers.
And for me specifically, how people interact, how people use computers to teach and learn
programming in data science.
Yeah, that sounds super fun.
There's just so many more interfaces these days.
You know, human-computer interfaces used to be just like, well, how do you use Windows?
Yeah.
Or something like that, right?
Well, even before that, it was like, how do you type stuff into a terminal, right?
Before we even had like Windows.
And then people from like the community was like, you know, like Windows would be good.
And then they were like, maybe touchscreens will be good.
So now we have all these cool input, ways to input like commands into a computer and make
use of that.
Yeah, absolutely.
And so it seems like you and Philip are somewhat focused on how developers interact with computers.
Is that fair to say?
We kind of do a mix.
So Philip, I think, has done a lot of developer-oriented work maybe like in the last five years or 10 years
or so.
For me specifically, I'm more interested in like how students and instructors use computers.
Okay.
So like what people, how people use, like create lecture slides using like screenshots of like their notebook environments
and how people like put code in their slides and use code to teach while also like, while also talking over at the same time.
Sure.
Well, and notebooks are all about communicating computational ideas, either being science or developer or data science, right?
Yeah.
Yeah, for sure.
And then nowadays we see a lot of notebooks being used in the classroom as well.
So instructors will not only have lecture slides, but a lot of instructors will like flip back and forth between lecture slides and a computational notebook like Jupyter.
So they'll have lecture slides for a few minutes and then they'll do some live coding in front of students for another few minutes and they'll switch back and forth.
So we're seeing a lot of that sort of use case in the classroom.
And that's where, like, that's the sort of thing that I'm really interested in because here we have people doing something visual, doing something verbal, but also now doing, working with code and having students like see code in the classroom.
I think it must be such an advantage for students these days.
I remember when I was in school, it was either the instructor would be writing something either on the blackboard or on like one of those overlay things with a pen and with a light that literally went through it.
Or, you know, you would just get a book or something.
But now if you want to say, well, look how these forces in physics come together or look how these chemical bonds are formed.
You could see them actually, you could see actual animation that you could try new ideas.
And it's just, it's opened up so much exploration, I think.
Yeah, I think it's really cool.
And, you know, I don't know about you, but I would definitely prefer to write code in the computer rather than writing code in the overhead slide.
Oh my goodness.
Can not even imagine doing it that way.
So for sure, for sure.
All right.
Speaking of code, let's talk about your project.
So you have this project called Pandas Tutor.
Yeah, that's right.
And this is a little bit of a next generation data science thing followed on from Python Tutor, right?
I see some similarities in the website and stuff like that.
Yeah, yeah.
So Pandas Tutor, the website is similar to the Python Tutor website because Philip used like his old style sheet for both of them.
Sure.
When we were getting this out the door, we were, at the time, we were like very focused on squashing bugs and like adding new features.
And then the website was like, oh, okay, like we got to launch this thing.
We need something to put on.
So Philip was like, okay, let me copy my styles.
Yeah.
Well, his styles look good.
So I think it's, there's nothing wrong with that.
It's very nice visualizations and interactives.
So what is this?
Why would people be interested in Pandas Tutor?
It might sound like it's a series of video courses on Pandas or something, but that's not at all what it is, right?
Tell people what this is.
Yeah.
So Pandas Tutor is a little website tool where you can paste in some Python Pandas code.
So Python code that works with data frames.
And what the tool will do is it'll take your code and break it down step by step.
It'll draw a little diagram for you at every step of your code.
So I have a little example on the website, but the example on the website basically shows that if you have a few lines of Pandas code, when you run those lines in a Jupyter notebook, let's say, all you get when you run the cell, all you get when you run the code is a final output.
So even if there are like four steps in the recipe, all you get is the finished cake and you don't really get to see the middle steps that happen.
And so often, you know, that's what you do.
Like I'm going to transform this data frame with these multiple operations to get a new data frame.
That's the destination I want to be at, right?
Totally.
Yeah.
And it happens all the time in real practice for data analysis.
And it also happens a lot in the classroom.
So a lot of times when I'm teaching in data science, a little piece of data science, I'll write a few lines of code.
But the problem is like students ask me like, oh, what happened?
Like there's a lot going on.
Like, can you show me like this step or like step three in the code or step four?
And I have to like manually comment out lines of code and display those data frames to students.
So this tool kind of emerged from that use case.
Yeah, absolutely.
And it's totally reasonable that you would comment out those lines.
But it's also more difficult still to see the transformation, right?
So if I say, well, what happens if I don't do, say, a group by, but I just get the previous result?
Well, then, you know, you kind of, how do you put that side by side with the other, the next step, right?
There's not a great mechanism for saying put these side by side and see the changes without like lots of scrolling and back and forth and stuff.
Totally.
Yeah.
So it's something, it's a use case that came directly out of our experiences teaching data science, where a lot of times when we teach, we're like, okay, in the top cell of a notebook, we'll like display dogs.
And then the cell below it, we'll do some stuff with the data frame, like dogs dot sort values.
And then we have to say, okay, students, like look at the top data frame and look at the bottom one, and then compare those two and like stare really hard at it and try to figure out what happened between the two, between the two data frames to understand it.
So whereas with Pandas Tutor, what I could do instead is put the code, just paste the code into Pandas Tutor.
It'll display the two data frames side by side and draw some arrows between the two data frames or add some coloring so that I can see, oh, okay, the rows were sorted.
I don't have to ask people to like stare really hard and imagine the rows moving from place to place.
And before we get too far into this episode, I do want to just point out to people like this is a really visual tool, which is its massive advantage.
But that also puts Sam and me at a disadvantage for discussing it during this audio, mostly audio presentation.
So you might consider checking out also the YouTube stream and flipping around there or just open up PandasTutor.com and play around with it yourself.
Like it's all about just a live example.
So with that said, I want to dive into a few things.
There's going to be an example here that we're going to cover a lot.
And it's an example using a Pandas data frame called dogs.
And the dogs are like the breed, which would be German Shepherd or Terrier or whatever.
And then type is a herding dog or a hound dog or a toy dog.
I love the toy dog idea.
And then there's a filtering statement where you say dogs bracket, dog size, equal, equal, medium.
That's a standard Pandas like, let's do this filter type of wear clause.
And then there's a sort where you sort by the type of dog, you know, the herding or hound dog or working dog, whatever.
And then you do a group by on the type.
So show me all the non-sporting dogs, all the sporting dogs and so on.
Yeah.
Yeah, that's right.
We'll be using this data quite a bit.
Before we get into analyzing Pandas, I guess one thing I was wondering is, it's super focused on Pandas and Pandas data frames.
Did you consider making it for other tools like a NumPy one or a TensorFlow one?
Or like why Pandas specifically out of the data science space?
It's a great question.
Pandas happens to be like the de facto tool for working with data tables in Pandas.
I mean, they can bring data tables in Python.
And it's taught a lot in introductory courses specifically.
So a lot of intro to data science courses, when they teach students how to work with data tables, they'll also teach students how they work with Pandas.
We chose to focus on Pandas because of like those two aspects.
So one, it's like a common standard package that a lot of people use in practice.
And it's also a package that people, a lot of people learn when they're learning data science for the first time.
They'll learn Pandas as one of the first stepping stones to learning how to do data analysis.
Now, there is another version of this tool for the R data, like R tidyverse world.
It's called a tidy data tutor instead of Pandas tutor.
And it was made by one of my lab mates and Phillip as well.
So we kind of, the three of us kind of worked together.
I worked on the Pandas side and then my lab mate worked on the R tidyverse side.
Sure.
Okay.
Yeah, that's cool that it depends, you know, regardless of which side of data science you're on, you can grab this and run with it.
Yeah.
And as for other tools, I think the approach that the tool uses to analyze code could be applied to all sorts of other tools, including tools like TensorFlow.
And that's probably one of the exciting things for me is that like, this for me is like a stepping stone towards like all sorts of visual tools for learning Python packages or other data science specific things that otherwise are difficult to understand.
Or like things that you would normally draw a picture to understand normally.
I can imagine a tool like this drawing a picture for you or helping you like when you're explaining your code to someone else.
When I first learned about this, Brian Okken and Leah Cole and I spoke about it over on the Python Bytes podcast.
And Leah made a really interesting point when she saw this.
My first impression is, okay, this is a great way to teach students who are getting into data science.
And it sounds like that's a very solid use case.
But Leah said, hey, this would be really good if you just like went to Stack Overflow or you picked up some new code and you're like, what does this do?
This is like a complicated pandas expression.
Let me throw it in here and you could visualize that.
So I want to sort of put that out there for people to think about, well, if they're not learning data science right now, they're probably still encountering algorithms and data sets that are new to them.
And that might be useful as well.
What do you think?
Yeah, I totally agree with that.
And actually, one of the ideas that Philippa and I had was to do a little bookmarklet where if you put the bookmarklet in your browser bookmarks bar and then you're on Stack Overflow and you click that button, we could even show you those diagrams that pandas created in line.
If you click it online on a Stack Overflow website or let's say the pandas documentation.
I've certainly run into the problem where I'm trying to like Google, like, how do I, let's say, unpivot a data frame and then I pull up the first Stack Overflow result and then it works, but it's like five lines of complicated machinery.
And I'm trying to figure out what's going on.
And I have to like, like kind of walk through it step by step myself to make sure it's not messing up my data in some way.
And so I can imagine like a tool like this, if you could display the visualizations in line and just put it in Stack Overflow's website itself on the user side, then that could be like a nice way to make use of the visualizations in real practice.
That would be really cool.
Just install a browser helper that like every time it detects a panda statement, it just puts a little.
Yeah, yeah.
Like a little Chrome extension.
That would be super neat.
I would use that.
Yeah, for sure.
So let's click on start visualizing your Python code now.
Now I'll pull this up and run.
And this, what happens for those of you who are not familiar with Python Tutor and Pandas Tutor is it actually executes, you put a block of code in here and it actually executes it in a container or something like that, right?
Yeah, that's right.
So it'll run your code in a Docker container on one of our servers.
Nice.
You don't have to just explore other people's code, right?
You can put whatever you want in here.
Now, the first thing that I think about when I think about data frames is data.
Obviously, you've got to get some data in here.
And as we were talking about this Chrome extension, one of the challenges for me is, well, here's a cool little code snippet, but it has to have the proper data frame data backing it before it really means anything, right?
Yeah, that's right.
It's a challenge.
It is.
So how do you get data in here?
What are the options for getting data in here?
Okay.
So the one challenge about hosting a tool that runs code for other people is that it's a little bit, you run into issues when you let the tool like also have internet access.
Because then people will start mining Bitcoins using X2.
Yes, exactly.
And arbitrary code on the internet, how could it go wrong?
I mean, I know with a Docker container, it's not going to harm you or the other things most likely, unless there's an exploit fully in Docker.
But they're going to exploit your computational resources and things like that, right?
That's right.
Yeah.
Okay.
So you probably don't have full just root access to do whatever you feel like here.
Yeah.
So the tool, it cuts off internet access for your code and it also imposes like a few memory limits.
So I don't think you can write, you might be able to read or write to disk temporarily.
I don't quite remember the details of that, but I do know that it does restrict internet access.
So a lot of things like one common way that we'll get data sets in class is we'll do pandas.readcsv and put in a URL.
Yeah.
Of a CSV file.
And unfortunately, that doesn't work for the tool.
So the way that we're getting around this right now is in the examples that we have for pandas tutor on the website, we have like inline CSVs.
So we put a snippet of a CSV file as a Python string, and then we can read that string into pandas as if it was a CSV file.
Yeah.
So you just drop it in as part of the code as a, what's that, a literal string, the triple quotes that'll go multiline.
So you just drop it in the middle of that, right?
Yeah.
And it's not, I definitely don't think it's an ideal solution.
What I would like, like one stepping stone to like a future solution would be to include some example data sets with Python, with pandas too, so people can load in directly.
That's a good idea.
The R version of this tool has some built-in data sets because of R's like built-in packages.
So R has like, I think it comes with default, like the car, like the cars data set, maybe like some flowers data sets.
So you can load those in directly.
And that makes the R version of this a little bit more convenient.
So we're thinking about ways of loading in data right now, but in terms of being able to load in arbitrary data, the only real option right now is to go into your CSV file and copy and paste out a few lines of it into the tool directly.
This portion of Talk Python to Me is brought to you by SignalWire.
Let's kick this off with a question.
Do you need to add multi-party video calls to your website or app?
I'm talking about live video conference rooms that host 500 active participants, run in the browser and work within your existing stack, and even support 1080p without devouring the bandwidth and CPU on your users' devices.
SignalWire offers the APIs, the SDKs, and edge networks around the world for building the realest of real-time voice and video communication apps with less than 50 milliseconds of latency.
Their core products use WebSockets to deliver 300% lower latency than APIs built on REST, making them ideal for apps where every millisecond of responsiveness makes a difference.
Now, you may wonder how they get 500 active participants in a browser-based app.
Most current approaches use a limited but more economical approach called SFU, or Selective Forwarding Units, which leaves the work of mixing and decoding all those video and audio streams of every participant to each user's device.
Browser-based apps built on SFU struggle to support more than 20 interactive participants.
So SignalWire mixes all the video and audio feeds on the server and distributes a single unified stream back to every participant.
So you can build things like live streaming fitness studios where instructors demonstrate every move from multiple angles, or even live shopping apps that highlight the charisma of the presenter and the charisma of the products they're pitching at the same time.
SignalWire comes from the team behind FreeSwitch, the open-source telecom infrastructure toolkit used by Amazon, Zoom, and tens of thousands of more to build mass-scale telecom products.
So sign up for your free account at talkpython.fm/signalwire, and be sure to mention talkpython to me to receive an extra 5,000 video minutes.
That's talkpython.fm/signalwire, and mention talkpython to me for all those credits.
It's not a huge problem because the goal here is not to execute and get results from it.
It's like, what would it look like if I did this?
Yeah, that's right.
So it's not really about visualizing gigabytes of data frame transformations because in those cases, we're going to draw a bajillion arrows, and it's really hard to see what's exactly happening between the two data frames.
Exactly.
From an understanding perspective, it might be worse, right?
Because it's like, as people will see as they explore PanasTutor.com, like, it's putting lines and little interactive widgets all over the place to see how the data flows.
And if you've got too much, then it's just lines everywhere.
Yeah, yeah.
Yeah, cool.
All right.
So we've got this data, which we already talked about a little bit, the dogs.
And so we've got like a Labrador Retriever, which is a sporting dog.
That is, it lives to 12 years.
That's right.
It's medium-sized, and it's 67 pounds, and so on.
So that's the kind of data that we're working with.
You jam that into an in-memory CSV string, use io.string.io to treat it like a file stream, and tell Pandas to read CSV it.
Okay.
So then you want to know, basically, let me go to the finalists.
You want to know, given a type of dog, like non-sporting, sporting, working, what is its median longevity in years and its weight, right?
That's right.
So that's the kind of question that you're trying to answer, which is kind of incredible that given this data, it's only three lines of Pandas to answer that.
Yeah, Pandas does a lot behind the scenes.
Yeah, that's pretty remarkable, actually.
So what happens when you say visualize this is it takes that same code block that we've been talking about, dogs, dog of size, equal, equal, medium, sort values, group by, median, and so on.
And you put that on the screen where the parts that are not relevant at the moment are grayed out, and the code that it's actually applying at that step is strong or regular color font.
That's really clever.
How do you come up with that idea to say, like, we're going to show the flow through this code by, like, dimming the other parts of the code?
When we were making this tool, our first cut of this was actually just to show the code that we were running itself.
Right, so you'd say, like, dog, bracket size, equal, equal, medium, or something like that, right?
That's exactly right.
So in the first version of this tool, we did, like, if we're filtering a data frame and then, like, sorting it, we would just show the code for filtering first.
But we showed all the code for filtering.
And then when we sort the data frame, then we would show the code for filtering and sorting because that's the snippet of code that we would execute or, like, visualize.
And it ended up being a little bit, it ended up being, like, less helpful, in our opinion, when we had, like, longer chains, especially.
Because when you have longer chains, it looks like there's, like, five things running, but really all we're visualizing is the last step.
So what the solution we kind of came around to was to say, okay, well, we'll just display all the code that we're visualizing, but bold, like, highlight the part that we're visualizing in this step.
So if there's four steps, then we'll have, like, four little diagram displays, and we'll highlight this, like, the individual steps that we're running at each time.
Yeah, there's a lot of subtleness in this tool that it takes a, you know, on first glance, you don't see exactly how much it's communicating, but there's a lot of nice touches like that.
So in this first step, it says we're going to go and filter down our data set from all the different types of dogs, like large dogs, small dogs, and so on, to just look at medium-sized breeds.
So then we get to the thing that we were talking about, where you've got two data frames, as if you had done just, like, df.head or something, where it puts the top and then, like, a few listings of them.
That's right.
But it puts them side by side, right?
Tell us what's on the screen here.
Describe this.
Yeah.
So on the left, we displayed the original data frame dogs, and then on the right, we displayed the data frame dogs, but with the medium dogs filtered.
So the right data frame only has medium dogs, and the left data frame has all the dogs in the original data frame.
Right.
So that's the filtering that you might first see.
But then you also have lines, which are pretty cool.
Yeah.
So what we're doing here is we're showing which rows from the original data frame made it into the right data frame.
So in the original data frame, it looks like rows three, four, you know, three through seven, and a few more other rows made it into the right side.
And what we're doing is we're drawing arrows to show from the left side which rows got copied over into the right side.
Yeah.
So in that filter, you can sort of see them going across, which is fantastic.
And the other part that's subtle, I guess there's two more, is this looks like a static picture.
Like, it almost looks like a JPEG or something.
But then as you move your mouse over, as you hover over a particular row in the original data set, it'll highlight just what happens to that row and where did it end up, either directly or in some kind of aggregation like a sum or something on the right-hand side, right?
That's right.
So there's this really cool sort of hovery, like, follow this piece of data along story.
Yeah, that's right.
And you can do it on the right side, too.
So if you hover on the right side, it'll show you the rows that went into that on the left side.
Okay.
This is convenient when there's a lot of arrows.
So this is, like, the main reason why we did this was in some cases, there are a lot of arrows, and you want to just show a few of them.
And to do that, the easiest way to do that in this tool right now is to highlight moving mouse over a row.
Yeah.
That's one subtlety here.
In this case, it's not super relevant because it's just a filter.
That's right.
When you get down to, like, group by and stuff like that, you're like, well, show me everything that went into this row on this group by.
Like, well, these three.
That's right.
That's pretty cool.
Yeah, yeah.
So it's especially helpful when you have, like, multiple rows on the left going into a single row on the right or some weird combination of rows that otherwise, like, would be hard to see.
Yeah, for sure.
One final thing is we're doing a sort by this comparison, or sorry, filter, by this comparison on the size column of the data frame.
So dog, bracket size, equal, equal, medium.
And on both of these, you've got, like, an outline, a vertical outline on the size.
So that's another aspect that's really nice to highlight what's going on here, right?
Yeah, that's right.
So we're drawing a box from the size column.
That's meant to show the user, like, what column we're using for filtering.
Yeah.
There's actually one more thing here I wanted to point out, which is that if you display this data frame in a Jupyter notebook, you might not actually see any medium-sized dogs because they're hidden in, like, the middle rows of this data frame.
Yeah.
And so by default, Pandas displays, like, a few rows from the top and the bottom of a data frame.
But sometimes when you're, like, filtering, the rows that you're filtering only come from the middle of the data frame, which are hidden by default.
So you have to kind of, like, really, like, scrub through the data frame yourself in a Jupyter notebook to see that there are medium dogs in the first place.
So one thing we're doing with this tool is whenever we draw an arrow, we make sure that the rows on the left are visible to the user.
So we'll, like, selectively hide and show rows to make sure that you can always see the rows that we're making use of.
Right.
The most important ones are the ones that are included.
So let's include some of them, right?
That's right.
Yeah.
You've got these little subtle one more, four more in this section, and so on.
So that's pretty neat.
All right.
So, so far, we're step one.
We've taken all the different types of dogs, and we're now down to a data frame of just medium-sized dogs.
The next thing to do was, what, sort them, right?
Yeah, that's right.
Okay.
So maybe tell us about this step here.
Yeah.
So in this step, we're sorting the rows by the type column.
So again, we're going to highlight or draw a box from the type column for the input and the output to show the user, this is the column we're using for this operation.
And here we're drawing arrows, and the arrows kind of crisscrossing this diagram because some of the rows go to the top of the data frame, and some of the rows go to the bottom of the data frame after we sort them.
Yeah.
As you would expect.
But you can see, after the sort, here's where everything sort of started and ended up.
So that's pretty straightforward, I would say.
And then you have a group by one.
Tell us how you visualize the group by stuff because that's pretty interesting to think about.
How do you represent the two-dimensional data, but then additionally the groups?
Yeah, yeah.
So when we group data, what we do visually is we use the same color to highlight the backgrounds of each row within a group.
So in this example, all the non-sporting dogs will get highlighted blue.
All the sporting dogs get highlighted light green.
And then all the working dogs get highlighted red.
So you can see visually how the rows are put together in groups by pandas.
Yeah, fantastic.
And then finally, simple but also very cool visualization is you just go to that data frame.
You say, give us the median of that.
And I guess, I don't know pandas super well, but I guess if you just say dot median, that'll just give you the median of all the columns that are numerical.
Is that what happens?
That's right.
So it'll give you the median for all the dog longevities and the median for all the dog weights.
Which is the two numerical columns.
Yep.
Okay.
And in this case, what we're doing instead is because we've grouped the data frame before taking the median, pandas does the median within each group.
So it'll find the median longevity and weight for the non-sporting dogs and then for the sporting dogs and then for the working dogs.
And so what we're doing in the diagram here is we're showing how the four non-sporting dogs on the left side get kind of aggregated together into one row on the right-hand side.
So there's four arrows going from like four rows on the left side that go into one row on the right.
Yeah, that's awesome.
And this is probably that case where showing where the lines go is most interesting because there's like the first four results are all non-sporting.
So they contribute to row one.
So you've got all these arrows going into that first group and so on.
That's right.
I think that really helps visualize how this is computed.
Yeah, and that's right.
And group by is especially a tricky thing when we teach students because students are like, I understand sorting because like, you know, the rows get moved around.
When we do group by and then do something afterward, all of a sudden like my rows disappear.
Like where did my rows go?
And so here we can see exactly where all the rows go.
Yeah, that is very tricky because often you don't see that group by intermediate representation.
Yeah, yeah.
And pandas actually does not help people understand the group by.
So in a notebook, if you do dot group by by itself, all you get back is like this pandas group by object at some memory address.
And that's for novices.
It's like, I don't know.
I have no idea what just happened.
And so what we decided to do here as kind of like a design step is instead of showing the default group by like display, which is just text, we expanded that out to the original data frame plus some colors.
Yeah, yeah.
It really is a great visualization.
So I guess while we're talking about setting up your code, one thing that's interesting here is I can go to the top and I can jam in my code and I could do, you know, maybe I could change this to like, I don't know, max.
Is that going to work?
I got a different result by running, you know, dot max.
And I might play and explore and so on.
And then I'm like, I want to save this.
I'm going to save it for myself or I want to share it on Twitter because I just got to say something on Twitter and let people check it out.
I want to put it in the gist.
I want to share with my students, whatever.
So you'll have a handy little thing at the bottom that lets you create sort of a shareable link type of thing.
How does that work?
Do you just base64 encode the stuff at the top into the URL or what happens?
It's actually even simpler than base64 encoding.
We just put the code verbatim into the URL.
If you're looking at the URL, like the shareable URL, like you'll see that we put the code like import pandas as PD into the URL there.
Yeah, that's true.
It's just HTML.
No, it's URL encoded.
It's just URL encoded.
Yeah.
So we don't do anything super special with it.
And this works because URLs can actually be pretty long.
Like browsers will accept pretty long URLs.
And so for a lot of cases, we actually can't put the entirety of the code in the URL of software sharing.
This portion of Talk Python to Me is brought to you by the Stack Overflow podcast.
There are few places more significant to software developers than Stack Overflow.
But did you know they have a podcast?
For a dozen years, the Stack Overflow podcast has been exploring what it means to be a developer and how the art and practice of software programming is changing our world.
Are you wondering which skills you need to break into the world of technology or level up as a developer?
Curious how the tools and frameworks you use every day were created?
The Stack Overflow podcast is your resource for tough coding questions and your home for candid conversations with guests from leading tech companies about the art and practice of programming.
From Rails to React, from Java to Python, the Stack Overflow podcast will help you understand how technology is made and where it's headed.
Hosted by Ben Popper, Cassidy Williams, Matt Kierninder, and Sierra Ford, the Stack Overflow podcast is your home for all things code.
You'll find new episodes twice a week wherever you get your podcasts.
Just visit talkpython.fm/stackoverflow and click your podcast player icon to subscribe.
And one more thing.
I know you're a podcast veteran and you could just open up your favorite podcast app and search for the Stack Overflow podcast and subscribe there.
But our sponsors continue to support us when they see results and they'll only know you're interested from Talk Python if you use our link.
So if you plan on listening, do use our link, talkpython.fm/stackoverflow to get started.
Thank you to Stack Overflow for sponsoring the show.
So people can come down here and they can copy this URL and then they can do whatever they want.
Either save it as a bookmark for themselves or share it, right?
That's cool.
Yeah, that's right.
So the idea is if I had, let's say, like an email I'm sending to a colleague and I send them a little picture of data frames, like with some arrows,
I can also send this URL, which will let the other person view like the original code I used to create the diagrams.
Sure. So maybe it makes sense to do like a snapshot screenshot of just one step and then you say, well, here you can explore the whole thing and rerun it and so on.
It also kind of leads to the reproducible publications, reproducible science.
You know, if you're trying to explain to people what this step does, it might be worth putting that in the paper, right?
Yeah, exactly.
Yeah. And one use case we imagined was like, let's say for a lecture slide, I'll take a screenshot of this page.
I can also put in the URL, like some small text at the bottom of the lecture slide so people can play around with it afterward.
You need a URL shortening service, like a pandas.toot.or slash 170.
Yeah, yeah.
I don't even know .ORs or domains, but they should be.
I don't know. Maybe.
But like a bit.ly equivalent of a short one. Yeah. Cool.
Yeah.
All right. Well, I do see there's a place I can suggest improvements. So like I can just talk to you and tell them.
Yeah, yeah. Just send us a little email.
Yeah. One thing we do want to do is to include like a, like save this diagram as a PNG or SVG button.
Oh, nice.
We haven't gone around to it, but we would like to include that at some point.
Yeah, that'd be great. You know what would be fantastic is save it as an animated GIF.
Yeah, that would be cool.
Where you could sort of see like the little arrows run by or something.
Yeah, that'd be fun.
That's a cool idea. Yeah. Yeah. Think about that.
Well, just give me more work. So super cool project. This project is part of your PhD work, right?
Yeah, that's right.
Yeah. What's next? Where's it going from here?
I'm looking to graduate like next year's spring. So my hope is that this project will be like a major piece of piece of the thesis and I'm going to talk about it a lot. And as for the tool next steps in the short term, we'd like to expand the types of visualizations it can do. So right now it doesn't know how to do joins, for instance, or pivots. And so we really like to include those because those also tend to be like really confusing for people learning.
Right. And they're in the group by category of hard to understand. Yeah.
Yeah. Yeah.
I think they're worse, actually.
Yeah. It's a toss up, I think, in my experience. People, students who struggle, students will always have one, at least one of those three to struggle with. Group buys, joins, and pivots. Those three are just really confusing.
And longer term for this tool, we're thinking it would be great to have it, as I alluded to a little bit earlier, have a version of it that we can embed into Stack Overflow or even the Pandas documentation itself. And we're also looking at different alternative ways of drawing arrows and colors to see what can help students. And then we're also looking at ways of applying this general code analysis approach to other tools for visualization.
Yeah. There's certainly other tools that could benefit from it, although not so many of them as popular as Pandas. But for example, like Dask. Dask is panda-like, but it's fairly complicated what happens to compute stuff.
Yeah.
I mean, not understanding the ideas of it necessarily, but it's like, okay, well, it's going to go after this cluster and these things happened. There could be interesting things there.
Yeah, totally. Like there's Dask and now there's like other tools like Ray and like, there's even, even like if we're going really far out, there's like tools like Spark and like MapReduce and stuff that distribute computing across different computers. Visualizing those would be really cool.
Because, you know, for me as a user of those tools, sometimes it's like when like one computer breaks, it's really hard to debug and figure out what happened.
It definitely is. Another one is just database queries.
Oh yeah, for sure.
Right. I mean, it's pretty similar. You've got like group buys, you've got sorting, you've got filtering, select where clauses. And yeah, I think that would be a pretty natural match as well. But yeah, there's a lot of places for it to go. So very cool. Are you using this in any of your courses and things like that?
At the moment, I'm not teaching any data science courses, but this summer I will be teaching a course at UC San Diego. And so I'm really hoping to use this tool to like make some little diagrams to copy and paste into my lecture slides. Before I would make these diagrams manually using like Google slides, like arrows and shapes and such. But it's a huge pain. It's a massive pain.
Oh gosh, I know. It's so painful. As somebody who does online courses, I'm like, I really want to put some arrows and show how this goes. Like, oh boy, this is a lot of work.
It's a lot of work. It's a lot of work.
Yeah. I've actually used a Python tutor in my beginner Python course to show people like, okay, when you have two variables, but they point to the same object in memory, here's why changing one of them changes the other, for example, because you're really the same. They're just like pointing at the same thing. So they're changing the thing they point at and whatnot. Pretty cool. So very nice.
Yeah. And you're also working on a book, right?
Yeah. It's a book. The website right now says principles and techniques of data science, but we're renaming the book to learning data science.
Okay, cool. Now, when is that coming out?
Ooh, that's a great question. A question to ask every book author.
Yes, I know.
We're really hoping to get a first cut of it by the end of this year. We're about like one third or halfway through the content right now.
But we're hoping to get like the first cut of it out by the end of this year and then going through the whole like editing cycle and like publication, like before publication work and have it published sometime in the year after.
Yeah. Fantastic. Well, good luck on that.
You know, we are getting a little bit short on time, mostly because I have an extra tight constraint today. Sorry about that.
No problem.
But let's talk a little bit about the internals. So we talked about uploading some code in the text area field of the website, how it runs in a Docker container. It has restrictions.
But how do you all pull this off? Like understanding some of these things that we're getting color and pictures and arrows for. I don't think that's built into Pandas or Python, is it?
No, it's not.
How do you pull this off?
Yeah. So what we're doing to actually make this work is we're parsing the code behind the scenes and then running each step like using a sort of like a debugger.
So what we're doing is we parse the code to split it up into the steps that we want to run. So in this case, we would split it up to say the first step is dogs and the second step is filtering. And the third step is sorting and then grouping and then taking the max.
So you don't go all the way down to like disassembly. Do you like disk dot disk? Because that many more steps than this. Okay.
Yeah. Yeah. So we're trying to keep it roughly at like the level of function calls. We just split up into its individual function calls. And in this case, we have like a slice syntax for filtering. So we split that up as well.
Okay. So then you basically run each step. You have the before data frame and the after data frame.
That's right. Yeah. So we run each step.
Unwound expression. Yeah.
We run each step, get like the left-hand side and the right-hand side. And then we essentially have some like special rules where if we do sort values, we'll like use this rule for drawing arrows.
If we see a group by, we'll use this rule for drawing arrows. So underneath the hood is really just a lot of heuristics and rules for specific pandas functions.
Yeah. So you might say, all right, if it's doing a group by and here's the groups, here's how we're going to understand which group this goes to.
So you're like, all right, it's grouping on the type column.
That's right.
So then you know which thing to point it to. I see.
Yeah, that's right. And we could have done a smarter approach using like some, like, I don't know,
some deeper co-analysis or machine learning.
But I think for teaching purposes, we really wanted to avoid the case where we draw arrows like wrongly.
So we draw arrows that are not supposed to be there.
Yeah.
That'd be confusing, wouldn't it?
Yeah. Yeah. And especially for learners, like we would rather just like not draw arrows and draw wrong arrows.
I think that was like our main, one of the main design decisions here.
So that's why we resorted to like a simple approach of just using like rules and heuristics, but it gets the job done.
Sure. What about source code? Can people come and check it out and play with it?
Or is it really they can just play with the website?
Right now, only the website's available for the public.
Like the source code right now, we're thinking about like making it open source.
But right now it's not really in a state for the rest of the world to see.
Uh-huh. What about the front end stuff here?
When I went and did a view source on it, there's not a lot to see.
Basically some JavaScript and like five or six empty locations on the page, right?
What's the story there?
Yeah. Yeah. So the front end code is a similar story as the back end code.
It's like pretty messy and very specific to our workflows right now.
So getting, so essentially the issue right now is getting it set up on someone else's computer would be like a massive pain.
That's one of the main issues with releasing the source code out is that if we were to release it right now as open source and people want to play around with it,
then we would probably get tons of emails from people asking how to like debug this one like really weird step that we're doing or how to make it work.
So that's something that we'd like to get around to.
But for the first release, unfortunately, we ran out of time and we just wanted to get the tool out the door.
Yeah, I know. That's the most useful bit is for people to go and play with it, right?
Yeah. Yeah.
Fantastic. Any chance of an offline version like a progressive web app?
So I actually would like a version of this tool that would work as a JupyterLab extension because JupyterLab has like a Python back end that we can make use of.
Yeah.
So I could totally imagine this being used like directly in a notebook and displaying the diagrams directly in a notebook without needing to go to planetary.com.
What about WebAssembly and PyIodide?
Oh, yeah, yeah. We thought about that too.
That's another like far-fetched idea we like to do.
We've seen like PyIodide right now is just super cool to me.
And I'd really like to do it.
And you could turn on like the internet accessibility and stuff again, right?
Totally.
Because it's all running on the cloud.
They're only going to hack themselves or use their own resources, right?
Yeah, yeah.
You know what?
That's great.
That's a great point.
I didn't even think about that.
But that totally makes sense.
It's coming along pretty well.
And Steve Dower was just telling us that they're starting to do official WebAssembly builds out of CPython.
So it's a little more stable than some other projects.
So I think there's progress in the WebAssembly space.
And the PyIodide guys have got some of the data science libraries compiled into it.
Yeah, possibly.
I don't know what that means in terms of your system that understands and takes it apart.
That could be totally tricky.
But maybe that's an option.
Yeah, I think it's really viable.
I really like to look into it in the months to come.
Yeah, for sure.
All right.
Well, Sam, I'm afraid that's all the time we got to talk about today.
So before, though, we get to move off of this, tell us about the final two questions.
If you're going to write some Python code, what editor are you using these days?
My editor right now is VS Code.
I've flipped.
I've jumped editors from like Notepad++ to like Sublime to like Vim and Emacs.
And now I'm settling into the VS Code world.
It's got me.
Yeah, cool.
Right on.
And then notable PyPI package.
Ooh.
Something you found, you're like, oh, that's really cool.
Honestly, I'm really vibing with TQDM right now.
I always forget how to pronounce it, but I really like that one.
It displays a little progress bar for looping.
And I just think it's a really nicely done package.
Yeah.
If you just do wrap, just do a decorator and you do a for loop or something like that.
I don't remember the exact API, but then you just do a for loop and it'll just automatically
do a progress bar.
That's right.
It's fantastic.
That's right.
I love it.
Yeah.
Super cool.
All right.
Final call to action.
People want to visualize their Pandas code.
What do you tell them?
I tell them, go to pandasvery.com and put your code in and see what comes out.
Awesome.
And I encourage people when you go there, try to interact with the diagrams, right?
There's a lot of stuff going on that it doesn't look at first interactive, but it is.
So yeah, go play around.
Sam, thanks for being on the show.
Thanks, Michael.
I'm great to have you here.
It was really great.
It was a lot of fun for me and I really enjoyed talking to you about it.
Same.
Bye.
Okay.
See you.
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.
Add high-performance, multi-party video calls to any app or website with SignalWire.
Visit talkpython.fm/SignalWire and mention that you came from Talk Python to Me to get started
and grab those free credits.
For over a dozen years, the Stack Overflow podcast has been exploring what it means to
be a developer and how the art and practice of software programming is changing the world.
Join them on that adventure at talkpython.fm/Stack Overflow.
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.
you
Thank you.