Query on syntax/code when using zip method in a loop

Screen Link:
Pandas Visualizations And Grid Charts | Dataquest

Example used by DQ:
days = [‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’]
traffic_per_day = {}
for i, day in zip(range(0, 135, 27), days):
each_day_traffic = traffic[i:i+27]
traffic_per_day[day] = each_day_traffic

My first query is regarding this bit of code - each_day_traffic = traffic[i:i+27]
Is it selecting the rows from the dataframe ? Not sure if i have seen that syntax earlier.

second query is for this code: for i, day in zip(range(0, 135, 27), days)
what is i referring to? - the rows within the range or day argument ?
I can’t wrap my head around how does the code know when to switch to next day, starting with first 27 instances belonging to Monday, next 27 belongs to Tue, and so on.


In the 2/14 screen it is said

The data was registered from 7:00 to 20:00 every 30 minutes.
The Hour (Coded) column has values from 1 to 27.

7:00 → 1
7:30 → 2

19:30 → 26
20:00 → 27

Basically every 27 gap batch represent data from monday to friday
index → (0-26, 27-53, 54-80, 81-107, 108-134)

Indeed traffic[i:i+27] is slicing the rows with the index (you can check out this code print(traffic.index, traffic[0:27])).

I think you were under the assumption that the data was collecting the whole day instead of just 7am → 8pm

When in doubts, the print function is your friend.
Hope it helps.

thanks! Being new to Python, it’s the zip function used with range which stumped me. Quite a few concepts being used along with for loop.

Zip is being used to a) iterate 2 lists together - this is required because we reverse thought about the pointers from c) to b) to a) mentioned here ; b) followed by merging the 2 lists as 1 list of tuples , each tuple with 2 values ; c) & finally converting the list of tuples to dictionary, where day of the week is the key and the 27 row dataframe is the value - to be called out for creating the graph for each day

Since zip uses 2 list objects as arguments ( as per current knowledge) , we created:

  • a list of days ( by manually writing down name of the day ) and
  • the second list of 27 row block * 5 times = 135 rows ( # rows could be determined by using df.shape or maybe info or describe methods too).

We need to make the code align every 27 rows against each day - which is what is known as iterating 2 lists together, before moving on to next set of 27 rows for the next day.

Writing the logic helps ! Hope it is helpful to others as well