Skip to content
0

SleepInc: Helping you find better sleep 😴

📖 Background

Your client is SleepInc, a sleep health company that recently launched a sleep-tracking app called SleepScope. The app monitors sleep patterns and collects users' self-reported data on lifestyle habits. SleepInc wants to identify lifestyle, health, and demographic factors that strongly correlate with poor sleep quality. They need your help to produce visualizations and a summary of findings for their next board meeting! They need these to be easily digestible for a non-technical audience!

💾 The data

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 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).

Acknowledgments: Laksika Tharmalingam, Kaggle: https://www.kaggle.com/datasets/uom190346a/sleep-health-and-lifestyle-dataset (this is a fictitious dataset)

import plotly.express as px
import pandas as pd
df = pd.read_csv('sleep_health_data.csv')
df
df.info()
df.isna().sum()
min_age = df["Age"].min()
print("Minimum age is:", min_age)
import plotly.express as px

# Create a histogram of Age
fig = px.histogram(
    df,
    x="Age",
    nbins=20,  # You can adjust number of bins as needed
    title="Age Distribution",
    labels={"Age": "Age", "count": "Number of People"},
    color_discrete_sequence=["lightblue"]
)

# Customize layout
fig.update_layout(
    height=500,
    xaxis=dict(showgrid=True, gridcolor="lightgray"),
    yaxis=dict(showgrid=True, gridcolor="lightgray"),
    plot_bgcolor="white"
)

fig.show()
average_sleep_by_age = df.groupby("Age")["Quality of Sleep"].mean()

# Convert to DataFrame for plotting
plot_data = average_sleep_by_age.reset_index()
plot_data
import plotly.express as px

# Create interactive line plot using Plotly
fig = px.line(
    plot_data,
    x="Age",
    y="Quality of Sleep",
    markers=True,
    title="Average Sleep Duration by Age",
    labels={"Age": "Age", "Quality of Sleep": "Average Quality Sleep"},
)

# Customize layout
fig.update_traces(line=dict(color='green'))
fig.update_layout(
    width=800,
    height=500,
    plot_bgcolor="white",
    xaxis=dict(showgrid=True, gridcolor="lightgray"),
    yaxis=dict(showgrid=True, gridcolor="lightgray")
)

fig.show()
average_sleep_by_gender = df.groupby("Gender")["Quality of Sleep"].mean()
average_sleep_by_gender
import plotly.express as px

# Convert to DataFrame for plotting
plot_data = average_sleep_by_gender.reset_index()

# Create a pie chart
fig = px.pie(
    plot_data,
    names="Gender",
    values="Quality of Sleep",
    title="Average Quality of Sleep by Gender"
)

# Customize appearance
fig.update_traces(textposition='inside', textinfo='percent+label')
fig.update_layout(showlegend=True)

fig.show()
average_sleep_by_Occupation= df.groupby("Occupation")["Sleep Duration"].mean()
average_sleep_by_Occupation
import plotly.express as px

# Convert to DataFrame for plotting
plot_data = average_sleep_by_Occupation.reset_index()

# Sort by sleep duration (optional, for cleaner visual)
plot_data = plot_data.sort_values(by="Sleep Duration", ascending=True)

# Create horizontal bar chart
fig = px.bar(
    plot_data,
    x="Sleep Duration",
    y="Occupation",
    orientation='h',
    title="Average Sleep Duration by Occupation",
    labels={"Sleep Duration": "Average Sleep Duration (hours)", "Occupation": "Occupation"},
    color="Sleep Duration"
)

# Customize layout
fig.update_layout(
    height=600,
    xaxis=dict(showgrid=True, gridcolor='lightgray'),
    plot_bgcolor='white'
)

fig.show()
‌
‌
‌