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.
| Column | Description |
|---|---|
Person ID | An identifier for each individual. |
Gender | The gender of the person (Male/Female). |
Age | The age of the person in years. |
Occupation | The 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 Category | The 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 Steps | The average number of steps the person takes per day. |
Sleep Disorder | The 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 pdsleep_data = pd.read_csv("sleep_health_data.csv")
sleep_data = sleep_data.drop_duplicates()
print(sleep_data)
print(sleep_data.columns)Sleep Duration
#Grouped summary statistics on Sleep Duration according to Occupation
occ_sleep_stats = sleep_data.groupby("Occupation")["Sleep Duration"].agg(['count','min','max','mean'])
#Sort values by mean in ascending order
occ_sleep_stats = occ_sleep_stats.sort_values('mean')
print(round(occ_sleep_stats,2))#Getting the 'mean' column.
occ_sleep_mean = occ_sleep_stats['mean']
#Converting from Series to DataFrame to allow column naming
occ_sleep_mean = pd.DataFrame(occ_sleep_mean)
#Renaming column: 'mean' -> 'Sleep Duration Mean'
occ_sleep_mean = occ_sleep_mean.rename(columns={'mean':'Sleep Duration Mean'})
print(round(occ_sleep_mean,2))Lowest average sleep duration
# Gets the occupation name only with the least average sleep
lowest_sleep_occ = occ_sleep_mean.iloc[0].name
# Gets the least average sleep value
lowest_sleep_occ_hr = float(occ_sleep_mean.iloc[0])
print(f"{lowest_sleep_occ} has the lowest average sleep duration, with only {lowest_sleep_occ_hr} hours.")Sleep Quality
#Grouped summary statistics on Sleep Quality according to Occupation
occ_sleepqual_stats = sleep_data.groupby("Occupation")["Quality of Sleep"].agg(['count','min','max','mean'])
#Sort values by mean in ascending order
occ_sleepqual_stats = occ_sleepqual_stats.sort_values('mean')
print(round(occ_sleepqual_stats,2))Lowest Average Sleep Quality
#Getting the 'mean' column.
occ_sleepqual_mean = occ_sleepqual_stats['mean']
#Converting from Series to DataFrame to allow column naming
occ_sleepqual_mean = pd.DataFrame(occ_sleepqual_mean)
# Gets the occupation name only with the worst sleep quality
lowest_sleep_quality_occ = occ_sleepqual_mean.iloc[0].name
# Gets the least average sleep value
lowest_sleep_quality_occ_score = float(occ_sleepqual_mean.iloc[0])
print(f"{lowest_sleep_quality_occ} has the lowest average sleep quality, scoring only {lowest_sleep_quality_occ_score}.")
Did the occupation with the lowest sleep duration also have the lowest sleep quality?
if lowest_sleep_occ == lowest_sleep_quality_occ:
same_occ = True
else:
same_occ = False
print(f"The occupation with the lowest sleep duration also have the lowest sleep quality: {same_occ}")