Sei sulla pagina 1di 11

27/11/2019 Top 3 Pandas Functions You Don't Know About (Probably)

Top 3 Pandas Functions You Don't


Know About (Probably)
Dario Radečić Follow
Nov 24 · 4 min read

I’ve written about Pandas before for obvious reasons — it’s an amazing
library for data analysis and even for data visualization. The earlier article
covered some functions you should be using more often because of they:

Will keep your code clean

Won’t make you reinvent the wheel

https://towardsdatascience.com/top-3-pandas-functions-you-dont-know-about-probably-5ae9e1c964c8 1/11
27/11/2019 Top 3 Pandas Functions You Don't Know About (Probably)

Photo by Tanner Boriack on Unsplash


https://towardsdatascience.com/top-3-pandas-functions-you-dont-know-about-probably-5ae9e1c964c8 2/11
27/11/2019 Top 3 Pandas Functions You Don't Know About (Probably)

If you’ve missed it, here a link to it so you can save it for when you have
time:

3 Pandas Functions You Should Be Using More Often


Stop reinventing the wheel. Seriously.
towardsdatascience.com

In today’s post, however, I want to shift the focus on a couple of more useful
functions that made me feel like a complete and utter idiot at work. Why is
that, you might be wondering. Well, I was lazy and I didn’t feel like googling
before I write code, so I was unaware those functions even existed.

The end result was, well, frustrating to say at least. I’ve managed to
implement the needed logic, but at the cost of a couple of hours, a handful
of new gray hair, and of course, a lot of unnecessary code.

To avoid the same happening to you, keep reading.

https://towardsdatascience.com/top-3-pandas-functions-you-dont-know-about-probably-5ae9e1c964c8 3/11
27/11/2019 Top 3 Pandas Functions You Don't Know About (Probably)

. . .

idxmin() and idxmax()


Yeah, I know I’ve covered those in the past already, but they are essential
for this post making sense. You won’t understand the second useful function
if you don’t understand these ones first, so let’s get to the point.

In a nutshell, those functions will return the ID (the index position) of the
desired entry. Let’s say that I create the following Pandas Series:

And want to find the index location of the smallest and largest item. Of
course, this isn’t hard to figure out just by looking at it, but you will never
(and I mean never) have so few data points on your projects.

What that means is that you’ll want to utilize the power of idxmin() and
idxmax() functions, and let’s see how:
https://towardsdatascience.com/top-3-pandas-functions-you-dont-know-about-probably-5ae9e1c964c8 4/11
27/11/2019 Top 3 Pandas Functions You Don't Know About (Probably)

Just keep in mind that the functions will return the index of the first
occurrence of the smallest/largest value.

. . .

ne()
This one was a big revelation to me. Some time back I was handling some
time-series data at work and had the problem where the first n observations
were 0.

For simplicity's sake, think of how you might have bought something but not
consumed it for a period of time. The item is in your possession, but as
you’re not using it, the consumption at the given date is 0. As I’m only

https://towardsdatascience.com/top-3-pandas-functions-you-dont-know-about-probably-5ae9e1c964c8 5/11
27/11/2019 Top 3 Pandas Functions You Don't Know About (Probably)

interested in the usage once you actually start to use the damn thing, ne()
was the function that saved the day.

Let’s consider the following scenario. You have a Pandas DataFrame object
with some observations that are 0 at the start:

Now what ne() would do, is to return True if the current value isn’t the one
you’ve specified (let’s say 0), and False otherwise:

https://towardsdatascience.com/top-3-pandas-functions-you-dont-know-about-probably-5ae9e1c964c8 6/11
27/11/2019 Top 3 Pandas Functions You Don't Know About (Probably)

This by itself isn’t useful much. Do you remember how I’ve said you need to
know idxmax() to understand the point of this? Well, I wasn’t lying, you
can chain idxmax() to the code from above:

So at index position 6, we have a first non-zero observation. Once again this


doesn’t provide much value to you. The good thing is that we can use this
information to subset the DataFrame to show only the values starting at
when the item was first used:

https://towardsdatascience.com/top-3-pandas-functions-you-dont-know-about-probably-5ae9e1c964c8 7/11
27/11/2019 Top 3 Pandas Functions You Don't Know About (Probably)

And this comes in handy so many times you’re dealing with time-series
data.

. . .

nsmallest() and nlargest()


I’m guessing that you can conclude just from the names what those two
functions are all about. Let’s say that I create the following DataFrame
object:

https://towardsdatascience.com/top-3-pandas-functions-you-dont-know-about-probably-5ae9e1c964c8 8/11
27/11/2019 Top 3 Pandas Functions You Don't Know About (Probably)

Just for fun let’s say that those 5 are observations of points achieved after
writing a test. You’re interested in finding out which 3 students performed
the worst:

Or which 3 students performed the best:

https://towardsdatascience.com/top-3-pandas-functions-you-dont-know-about-probably-5ae9e1c964c8 9/11
27/11/2019 Top 3 Pandas Functions You Don't Know About (Probably)

Those two functions are a nifty substitute for functions like sort_values().

. . .

Before you go
As time goes by I’m sure I’ll encounter many more time-saving functions
worth mentioning in an article, so you can definitely expect more posts like
this. In the meantime, please feel free to share your favorite Pandas
function(s) in the comment section.

Thanks for reading. Take care.

Data Science Machine Learning Python Programming Arti cial Intelligence

Discover Medium Make Medium yours Become a member


Welcome to a place where words matter. Follow all the topics you care about, and Get unlimited access to the best stories
On Medium, smart voices and original we’ll deliver the best stories for you to on Medium — and support writers while
your homepage and inbox. Explore you’re at it. Just $5/month. Upgrade
https://towardsdatascience.com/top-3-pandas-functions-you-dont-know-about-probably-5ae9e1c964c8 10/11
27/11/2019 Top 3 Pandas Functions You Don't Know About (Probably)

ideas take center stage - with no ads in


sight. Watch

About Help Legal

https://towardsdatascience.com/top-3-pandas-functions-you-dont-know-about-probably-5ae9e1c964c8 11/11

Potrebbero piacerti anche