Skip to content

Instantly share code, notes, and snippets.

@Ze1598
Last active January 24, 2021 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ze1598/59e62de843c5c74a4c81605ef60424cf to your computer and use it in GitHub Desktop.
Save Ze1598/59e62de843c5c74a4c81605ef60424cf to your computer and use it in GitHub Desktop.
Python Data Analysis Part 3: Education of Respondents
import pandas as pd
from os import getcwd, path
import plotly.express as px
import plotly.offline as pyo
pyo.init_notebook_mode()
path_to_data = path.join(getcwd(), "data", "survey_results_public.csv")
data = pd.read_csv(path_to_data)
data = data[["EdLevel"]]
data = data.dropna()
print(f"Rows left: {data.shape[0]:,}")
data.loc[:, "EdLevel"] = data.loc[:, "EdLevel"].map(
lambda response:
"Master's degree" if (response == "Master’s degree (M.A., M.S., M.Eng., MBA, etc.)")
else "Bachelor's degree" if (response == "Bachelor’s degree (B.A., B.S., B.Eng., etc.)")
else "High School" if (response == "Secondary school (e.g. American high school, German Realschule or Gymnasium, etc.)")
else "Professional degree" if (response == "Professional degree (JD, MD, etc.)")
else "Associate degree" if (response == "Associate degree (A.A., A.S., etc.)")
else "Higher Ed. study w/o degree" if (response == "Some college/university study without earning a degree")
else "Doctoral degree" if (response == "Other doctoral degree (Ph.D., Ed.D., etc.)")
else "Elementary school" if (response == "Primary/elementary school")
else "No formal education" if (response == "I never completed any formal education")
else response
)
ed_level_counts = data["EdLevel"].value_counts(ascending=True)
fig = px.bar(
ed_level_counts,
title="Education Level of Respondents",
orientation="h"
)
fig.update_layout(
xaxis_title = "Frequency",
yaxis_title = "Education Level",
title_x = 0.5,
showlegend = False
)
fig.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment