Skip to content

Your client, SleepInc, has shared anonymized sleep data from their hot new sleep tracking app SleepScope. As their data science consultant, your mission is to analyze the lifestyle survey data with Python to discover relationships between exercise, gender, occupation, and sleep quality. See if you can identify patterns leading to insights on sleep quality.

💾 The data: sleep_health_data.csv

SleepInc has provided you with an anonymized dataset of sleep and lifestyle metrics for 374 individuals. This dataset contains average values for each person calculated over the past six months. The data is saved as sleep_health_data.csv.

The dataset includes 13 columns covering sleep duration, quality, disorders, exercise, stress, diet, demographics, and other factors related to sleep health.

ColumnDescription
Person IDAn identifier for each individual.
GenderThe gender of the person (Male/Female).
AgeThe age of the person in years.
OccupationThe occupation or profession of the person.
Sleep Duration (hours)The average number of hours the person sleeps per day.
Quality of Sleep (scale: 1-10)A subjective rating of the quality of sleep, ranging from 1 to 10.
Physical Activity Level (minutes/day)The average number of minutes the person engages in physical activity daily.
Stress Level (scale: 1-10)A subjective rating of the stress level experienced by the person, ranging from 1 to 10.
BMI CategoryThe BMI category of the person (e.g., Underweight, Normal, Overweight).
Blood Pressure (systolic/diastolic)The average blood pressure measurement of the person, indicated as systolic pressure over diastolic pressure.
Heart Rate (bpm)The average resting heart rate of the person in beats per minute.
Daily StepsThe average number of steps the person takes per day.
Sleep DisorderThe presence or absence of a sleep disorder in the person (None, Insomnia, Sleep Apnea).
# import pandas package
import pandas as pd

df = pd.read_csv('sleep_health_data.csv')
print(df.head(), '\n')

df.shape
# dataset overview

print(df.columns, '\n', df.info())
# Check records with NAs

df.isna().sum()
# Check records with NULLs

df.isnull().sum()

1. Which occupation has the lowest average sleep duration?

# Groupby occupation and calculate mean sleep duration 

lowest_sleep_duration = df.groupby('Occupation').agg({'Sleep Duration':'mean'})
lowest_sleep_duration

# alternative:
# lowest_sleep_duration = df.groupby('Occupation')['Sleep Duration'].mean()
# Get occupation with lowest average sleep duration

lowest_sleep_occ_df = lowest_sleep_duration.loc[lowest_sleep_duration.idxmin()]
print(lowest_sleep_occ_df, '\n')

lowest_sleep_occ = lowest_sleep_duration.loc[lowest_sleep_duration.idxmin()].index[0]
lowest_sleep_occ

# alternative:
# lowest_sleep_occ = lowest_sleep_duration.sort_values().index[0]

2. Which occupation had the lowest quality of on average? Did the occupation with the lowest sleep duration also have the worst sleep quality?

# Groupby occupation and calculate average sleep quality

lowest_sleep_quality = df.groupby('Occupation').agg({'Quality of Sleep':'mean'})
lowest_sleep_quality
# Get occupation with lowest average sleep quality 

lowest_sleep_quality_occ_df = lowest_sleep_quality.loc[lowest_sleep_quality.idxmin()]
print(lowest_sleep_quality_occ_df, '\n')

lowest_sleep_quality_occ = lowest_sleep_quality.loc[lowest_sleep_quality.idxmin()].index[0]
lowest_sleep_quality_occ
# Compare occupation with the least sleep to occupation with the lowest sleep quality

result = lowest_sleep_quality_occ_df.iloc[0].equals(lowest_sleep_occ_df.iloc[0])
print(result)
# Compare occupation with the least sleep to occupation with the lowest sleep quality

if lowest_sleep_occ == lowest_sleep_quality_occ:
  same_occ = True
else:
  same_occ = False
# Compare occupation with the least sleep to occupation with the lowest sleep quality

assert lowest_sleep_occ == lowest_sleep_quality_occ