Skip to content
Search
Generic filters
Exact matches only

10 tips to make your data science code cleaner and more efficient

Concerning the formatting of the code, the structure of Python requires us to be very meticulous in order to format our code properly. Indeed, the number of lines to be skipped varies according to what is declared in the code (do we declare a class, a method, a variable?).

Lists should not be too long, otherwise, you have to go back to the line, the same goes for line length, space rules etc… In short, there are a lot of rules to follow and it is much too long to follow them meticulously by hand (see https://www.python.org/dev/peps/pep-0008/).

Fortunately, some libraries automatically format your code and the time saved is considerable. The best-known libraries are Black and Yapf and I highly recommend them. They integrate easily into an IDE like PyCharm and the result is really good.

Also, a good IDE can highlight major style errors and may suggest modifications. For my part, I use PyCharm and the correction system is very efficient.

The code below shows you how to transform badly formatted ugly code into well-formatted ugly code.