Skip to content

Instantly share code, notes, and snippets.

@tpatch
Created February 9, 2016 05:09
Show Gist options
  • Save tpatch/a44e78410ed29ca4e68b to your computer and use it in GitHub Desktop.
Save tpatch/a44e78410ed29ca4e68b to your computer and use it in GitHub Desktop.
For online data visualization course
import pandas
import numpy
pandas.set_option('display.float_format', lambda x: '%f'%x)
data = pandas.read_csv('datasets/ool_pds.csv', low_memory=False)
data.columns = map(str.upper, data.columns)
print(len(data))
print(len(data.columns))
data["W1_A11"] = data["W1_A11"].convert_objects(convert_numeric=True)
data["W1_K1_B"] = data["W1_K1_B"].convert_objects(convert_numeric=True)
data["W1_K1_A"] = data["W1_K1_A"].convert_objects(convert_numeric=True)
data["W1_K1_C"] = data["W1_K1_C"].convert_objects(convert_numeric=True)
data["W1_K1_D"] = data["W1_K1_D"].convert_objects(convert_numeric=True)
data["W1_B4"] = data["W1_B4"].convert_objects(convert_numeric=True)
# Pull and print variables
recode1 = {1:0, 2:1, 3:2, 4:3, 5:4, 6:5, 7:6, 8:7}
print("Days of week Watching News")
# news = data["W1_A11"].value_counts(sort=True, normalize=True)
data["W1_A11"] = data["W1_A11"].replace(-1, numpy.nan)
c1 = data["W1_A11"].map(recode1)
news = c1.value_counts(sort=True, normalize=True, ascending=True, dropna=True)
print(news)
# Break tv watching days into groups
recodeFreq = {"(0, 2]": "0-1 Days", "(2, 4]": "2-3 Days", "(4, 6]": "4-5 Days", "(6, 8]": "6-7 Days"}
data["TVDAYSFREQ"] = pandas.cut(data.W1_A11, [0, 2, 4, 6, 8])
c1b = data["TVDAYSFREQ"].map(recodeFreq)
daysFreq = c1b.value_counts(normalize=True, dropna=True, sort=True, ascending=True)
print("Days of week Watching News - Binned")
print(daysFreq)
recode2 = {1: "Just About Always", 2: "Most of the Time", 3: "Only Some of the Time", 4: "Never"}
print("How much do you think you can trust the police?")
data["W1_K1_B"] = data["W1_K1_B"].replace(-1, numpy.nan)
c2 = data["W1_K1_B"].map(recode2)
police = c2.value_counts(sort=True, normalize=True, ascending=True, dropna=True)
print(police)
print("How much do you think you can trust the government?")
data["W1_K1_A"] = data["W1_K1_A"].replace(-1, numpy.nan)
c3 = data["W1_K1_A"].map(recode2)
govt = c3.value_counts(sort=True, normalize=True, ascending=True, dropna=True)
print(govt)
print("How much do you think you can trust the Legal System?")
data["W1_K1_C"] = data["W1_K1_C"].replace(-1, numpy.nan)
c4 = data["W1_K1_C"].map(recode2)
lsystem = c4.value_counts(sort=True, normalize=True, ascending=True, dropna=True)
print(lsystem)
print("How much do you think you can trust the Public Schools?")
data["W1_K1_D"] = data["W1_K1_D"].replace(-1, numpy.nan)
c5 = data["W1_K1_D"].map(recode2)
pschool = c5.value_counts(sort=True, normalize=True, ascending=True, dropna=True)
print(pschool)
recodeAnger = {1: "Extremely Angry", 2: "Very Angry", 3: "Somewhat Angry", 4: "A Little Angry", 5: "Not Angry at All"}
print("Generally speaking, how angry do you feel about the way things are going in the country these days?")
data["W1_B4"] = data["W1_B4"].replace(-1, numpy.nan)
c6 = data["W1_B4"].map(recodeAnger)
anger = c6.value_counts(sort=True, normalize=True, ascending=True, dropna=True)
print(anger)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment