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).
# Start coding here
# Use as many cells as you need
import pandas as pd
df = pd.read_csv('sleep_health_data.csv')
df.head(10)
df.info()
df.describe()
df.isna().any()
df.isna().sum()
pd.set_option('display.max_columns', None)

# Which occupation has the lowest average sleep duration? Save this in a string variable called lowest_sleep_occ.
# Fixing the error by calculating the mean sleep duration for each occupation and then finding the one with the lowest value.
# Ensure the column names are correct
occupation_sleep_mean = df.groupby('Occupation')['Sleep Duration'].mean()
print(occupation_sleep_mean)
#occupation_sleep_mean.plot(kind='bar')

# Find the occupation with the lowest average sleep duration
lowest_sleep_occ = occupation_sleep_mean.sort_values().iloc[0:1].index[0]
print(lowest_sleep_occ)

# Find the occupation with the highest average sleep duration
highest_sleep_occ = occupation_sleep_mean.sort_values(ascending=False).iloc[0:1].index[0]
print(highest_sleep_occ)

# Which occupation has the lowest average sleep quality? Save this in a string variable called lowest_sleep_quality_occ. Did the occupation with the lowest sleep duration also have the lowest sleep quality? If so assign a boolean value to variable same_occ variable, True if it is the same occupation, and False if it isn't.
occupation_sleep_quality = df.groupby('Occupation')['Quality of Sleep'].mean()
print(occupation_sleep_quality)
occupation_sleep_quality.plot(kind='bar')

lowest_sleep_quality_occ = occupation_sleep_quality.sort_values().iloc[0:1].index[0]
print(lowest_sleep_quality_occ)

highest_sleep_quality_occ = occupation_sleep_quality.sort_values(ascending=False).iloc[0:1].index[0]
print(highest_sleep_quality_occ)

# Check if the occupation with the lowest sleep duration is the same as the one with the lowest sleep quality
same_occ = (lowest_sleep_occ == lowest_sleep_quality_occ)
print(same_occ)

same_occ_highest = (highest_sleep_occ == highest_sleep_quality_occ)
print(same_occ_highest)

# Let's explore how BMI Category can affect sleep disorder rates. Start by finding what ratio of app users in each BMI Category have been diagnosed with Insomnia. Create a dictionary named: bmi_insomnia_ratios. The key should be the BMI Category as a string, while the value should be the ratio of people in this category with insomnia as a float rounded to two decimal places. Here is an example:
normal_insomnia = df[(df['BMI Category'] == 'Normal') & (df['Sleep Disorder'] == 'Insomnia')]
print(normal_insomnia)

print(df['BMI Category'].unique())

obese_insomnia = df[(df['BMI Category'] == 'Obese') & (df['Sleep Disorder'] == 'Insomnia')]
print(obese_insomnia)

overweight_insomnia = df[(df['BMI Category'] == 'Overweight') & (df['Sleep Disorder'] == 'Insomnia')]
print(overweight_insomnia)

# Total Normal BMI Category
total_normal = len(df[df['BMI Category'] == 'Normal'])
print(total_normal)

# Total Obese BMI Category
total_obese = len(df[df['BMI Category'] == 'Obese'])
print(total_obese)

# Total Overweight BMI Category
total_overweight= len(df[df['BMI Category']== 'Overweight'])
print(total_overweight)

# Normal and Insomnia count
normal_insomnia_len=len(normal_insomnia)
print(normal_insomnia_len)

# Obese and Insomnia count
obese_insomnia_len=len(obese_insomnia)
print(obese_insomnia_len)

# Overweight and Insomnia count
overweight_insomnia_len=len(overweight_insomnia)
print(overweight_insomnia_len)

# Calculate Ratios
ratio_normal = round((normal_insomnia_len / total_normal), 2) if total_normal > 0 else 0
ratio_obese = round((obese_insomnia_len / total_obese), 2) if total_obese > 0 else 0
ratio_overweight = round((overweight_insomnia_len / total_overweight), 2) if total_overweight > 0 else 0

print(ratio_normal, ratio_obese, ratio_overweight)

bmi_insomnia_ratios={
    "Normal": ratio_normal,
    "Overweight": ratio_overweight,
    "Obese": ratio_obese
}
print(bmi_insomnia_ratios)