Creating Workout Data with for loops

Generating Workout Data

Generating Workout Data

In this post, I'll be revisiting a piece of Python code I wrote some time ago. The goal of the code was to generate a dataset representing workout repetitions over specific days and hours for a few months.

Code Breakdown

1. Module Imports

import random
import pandas as pd

Here, I'm importing the random module for generating random workout repetitions and pandas, a powerful library for data manipulation and analysis.

2. Setting up the Calendar Data

days_in_month = {
  1: 31, # January
  2: 28, # February
  3: 31, # March
  4: 30 # April
}

I initialized a dictionary called days_in_month to represent the actual number of days in each of the first four months of the year.

3. Data Generation

m = []
d = []
h = []

These are empty lists set up to store the month, day, and hour data, respectively.

for i in range(1, 5):
  for j in range(1, days_in_month[i] + 1):
    for k in range(7, 12):
      m.append(i)
      d.append(j)
      h.append(k)

Nested loops are used to iterate through each month, day, and hour to generate data. The loops ensure that the day values are accurate for each month, thanks to our days_in_month dictionary.

4. Random Repetition Generation

random_values = [random.randint(0, 50) for _ in range(len(m))]

I utilized a list comprehension with the randint function to generate random workout repetitions between 0 and 50 for each day-hour combination.

5. Data Compilation with Pandas

workout_data = pd.DataFrame({
  'month': m,
  'day': d,
  'hour': h,
  'reps': random_values
})

Finally, I created a pandas DataFrame called workout_data. This structure will hold our generated data in a tabular form, making it easy to analyze and visualize if needed.

This code served as a foundational step in generating synthetic workout data, and I hope this revisit serves as both a reminder and a tool for deeper understanding.

Comments

Popular posts from this blog

Drawing Tables with ReportLab: A Comprehensive Example

Blog Topics

DataFrame groupby agg style bar