Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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:
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)
If you’ve missed it, here a link to it so you can save it for when you have
time:
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.
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)
. . .
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:
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.
. . .
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:
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.
https://towardsdatascience.com/top-3-pandas-functions-you-dont-know-about-probably-5ae9e1c964c8 11/11