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 
import numpy as np 
import matplotlib.pyplot as plt
import seaborn as sns

sleep_data = pd.read_csv("sleep_health_data.csv")
sleep_data.isna().sum()


Which occupation has the lowest average sleep duration?
sleep_duration = sleep_data.groupby("Occupation")['Sleep Duration'].mean()
lowest_sleep_occ = sleep_duration.sort_values().index[0]
lowest_sleep_occ
sns.set_style("whitegrid")
g = sns.barplot(x='Occupation',
           y='Sleep Duration',
           data=sleep_data,
            ci=None)
plt.xticks(rotation = 90)
g.set_title("Average Sleep By Occupation")
g.set(xlabel='Occupation',
     ylabel='Average Sleep Duration')
plt.plot()
Which occupation has the lowest average sleep quality?
sleep_quality = sleep_data.pivot_table(values='Quality of Sleep',
                                    index='Occupation')

lowest_sleep_quality_occ = sleep_quality.sort_values('Quality of Sleep',ascending=True).index[0]
lowest_sleep_quality_occ
sns.set_style('whitegrid')
sns.set_palette('RdBu')
sns.set_context('paper')
g = sns.barplot(x='Occupation',
           y='Quality of Sleep',
           data=sleep_data,
           ci=None)
g.set(xlabel='Occupation',
     ylabel='Average Quality Of Sleep')
plt.xticks(rotation=90)
g.set_title("Quality Of Sleep By Occupation",y=1.03)
plt.show()
if lowest_sleep_occ==lowest_sleep_quality_occ:
    same_occ=True
else:
    same_occ=False
sleep_data['BMI Category'].value_counts()
How BMI Category can affect sleep disorder rates
print(len(sleep_data[sleep_data['BMI Category']=='Normal']))
normal=sleep_data[(sleep_data['BMI Category']=='Normal')&(sleep_data['Sleep Disorder']=="Insomnia")]

total_normal = len(sleep_data[sleep_data['BMI Category']=='Normal'])


normal_insomnia_ratio = round(len(normal)/total_normal,2)

obese= sleep_data[(sleep_data['BMI Category']=='Obese')&(sleep_data['Sleep Disorder']=="Insomnia")]

total_obese = len(sleep_data[sleep_data['BMI Category']=='Obese'])

obese_insomnia_ratio = round(len(obese)/total_obese,2)

overweight = sleep_data[(sleep_data['BMI Category']=='Overweight')&(sleep_data['Sleep Disorder']=="Insomnia")]
total_overweight = len(sleep_data[sleep_data['BMI Category']=='Overweight'])
overweight_insomnia_ratio = round(len(overweight)/total_overweight,2)

print(normal_insomnia_ratio)
print(obese_insomnia_ratio)
print(overweight_insomnia_ratio)
bmi_insomnia_ratios = {
    "Normal":normal_insomnia_ratio,
    "Overweight":overweight_insomnia_ratio,
    "Obese":obese_insomnia_ratio}
bmi_insomnia_ratios