And why I (and you as well) have probably been writing them inefficiently

Simple if else statements are a staple of every programming language. With Numpy and Pandas introducing unparalleled speed to Python enthusiasts, it was clear that people were going to need to create an if-elif-else feature that could be vectorized and efficiently applied to any specific column in your dataset. However, even though this would seem to be the case, if you actually google the best method of doing this… there really is no clear answer. Some users claim using loc and iloc are the fastest, others claim that pandas.where() is fastest , and some users even claim that list comprehension is the simplest and most efficient method. There is no proof to these claims, just users sharing the method they use to accomplish this seemingly simple task. Because of this, I scavenged the internet testing every single method that could accomplish the task, and the winner was not at all what I initially expected.

#data-science #if-else #python-programming #pandas #optimization #the most efficient if-elif-else in pandas

The Most Efficient if-elif-else in Pandas
1.40 GEEK