J

Code Style

The Python Enhancement Proposal 8 (PEP8) discusses the official Python code style guide: https://peps.python.org/pep-0008/.

Reading through the style guide is a good way to learn the syntax of a language. Just keep in mind that you do not need to adhere to every single rule.

Tools like Black1 have been created for Python so your code can be automatically formatted. This is useful so you can have the tool do your formatting for you, and it’s one thing less for you to worry about.

1. https://github.com/psf/black

While writing this book, I used the online black playground, to format some of the code: https://black.vercel.app/. Not every piece of code in the book follows PEP8 or Black. Sometimes, the code puts in additional line breaks to emphasize the code being taught.

J.1 Line Breaks in Code

Writing analysis code does get very wide at times. An additional constraint in the book is that the code needs to be even more narrow compared to the PEP8 rules.

There are two ways you can break up wide lines of code.

  1. Using the at the end of a line to tell Python that the code continues on the next line

  2. Wrapping your entire statement around a pair of round parentheses ( )

Let’s use the example from Section 4.3.

import pandas as pd
weather = pd.read_csv('data/weather.csv')

The first step in tidying up the data set was to call the .melt() method.

# this code is wide and will run off the page
weather_melt = weather.melt(id_vars=["id", "year", "month", "element"],
var_name="day", value_name="temp")

This ends up being a wide line of code. So we can put in line breaks between the round parenthesis of the .melt() method call.

# previous line of code can be rewritten as
weather_melt = weather.melt(
  id_vars=["id", "year", "month", "element"],
  var_name="day",
  value_name="temp",
)

In Pandas, many of the methods can be chained together (Appendix U). A common practice is to put each method call on its own line. This way if your eyes look down a straight line, you can get a rough overview of all the steps your data is going through. However, just putting arbitrary line breaks outside of a function call does not work.

# this will error, putting line break before the .melt
# previous line of code can be rewritten as
weather_melt = weather
  .melt(
    id_vars=["id", "year", "month", "element"],
    var_name="day",
    value_name="temp")
IndentationError: unexpected indent (3804754158.py, line 4)

We can solve this by using one of the techniques listed above

# use a  at the end of the line
weather_melt = weather 
  .melt(
    id_vars=["id", "year", "month", "element"],
    var_name="day",
    value_name="temp")
# wrap the entire statement around ( )
weather_melt = (weather
  .melt(
    id_vars=["id", "year", "month", "element"],
    var_name="day",
    value_name="temp")
)

The ( ) method is the style you will see more often reading Pandas code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.131.38.14