%matplotlib inline import pandas as pd import numpy as np import matplotlib.pyplot as plt import os file_name = "flying-etiquette.csv" data = pd.read_csv(file_name) data.head() data["Household Income"].value_counts() gender_edu_demos = data.groupby("Location (Census Region)") questions = gender_edu_demos.first().columns for q in questions: if q not in ["RespondentID","Questions"]: df = pd.DataFrame() for name, group in gender_edu_demos: df[name] = group[q].value_counts(normalize=True) df.plot(kind="barh", title=q) plt.show() rude_vs_freq = data.groupby("How often do you travel by plane?")["In general, is itrude to bring a baby on a plane?"] rude_vs_freq.value_counts().plot(kind='barh') create_all_graphs(data) df = pd.DataFrame() q = "In general, is itrude to bring a baby on a plane?" travel_data = data.groupby("Location (Census Region)")[q] for location, group in travel_data: if location == "Pacific": df['West Coast'] = group.value_counts(normalize=True) else: try: df["Rest of Nation"].append(group.value_counts(normalize=True)) except KeyError: df["Rest of Nation"] = group.value_counts(normalize=True) df.plot(kind='barh',title=q, figsize=(10.67,5.33)) #title = "charts/" + "01-" + q.replace(" ","_") + "_results_.png" #plt.savefig(title,format="png") ## helper functions here ## def create_all_graphs(data): for index, question in enumerate(data.columns): if question not in ["RespondentID","Questions"]: data.icol(index).value_counts(normalize=True).plot(kind='barh',title=question, figsize=(10.67,5.33) ) title = "charts/" + question.replace(" ","_") + "_results.png" plt.savefig(title,format="png") plt.show()