Skip to content

Instantly share code, notes, and snippets.

@acbass49
Last active April 12, 2025 20:07
Show Gist options
  • Select an option

  • Save acbass49/a1c0b5e47ea10c9d4d59a6d0bbb16271 to your computer and use it in GitHub Desktop.

Select an option

Save acbass49/a1c0b5e47ea10c9d4d59a6d0bbb16271 to your computer and use it in GitHub Desktop.
Christian Nationalism and Mormons
import pandas as pd
import numpy as np
import survey_tools as st
data = pd.read_spss("../data/CN/CN.sav")
data \
.query("Q22 == 'Mormon'") \
.shape
# There are 90 Mormons in the data set
data["Q22_rc"] = data["Q22"].astype("string")
data["Q22_rc"] = np.where((data["Q22_rc"] == "Evangelical or Protestant Christian (Baptist, Lutheran, Methodist, Presbyterian, Episcopalian, Pentecostal, Church of Ch") & (data["Q23"] == "Yes"), "Evangelical Christian", data["Q22_rc"])
data["Q22_rc"] = np.where((data["Q22_rc"] == "Evangelical or Protestant Christian (Baptist, Lutheran, Methodist, Presbyterian, Episcopalian, Pentecostal, Church of Ch") & (data["Q23"] == "No"), "Mainline Christian", data["Q22_rc"])
data["Q22_rc"] = np.where((data["Q22_rc"] == "Evangelical or Protestant Christian (Baptist, Lutheran, Methodist, Presbyterian, Episcopalian, Pentecostal, Church of Ch") & (data["Q23"] != "No") & (data["Q23"] != "Yes"), np.nan, data["Q22_rc"])
data["Q22_rc"] = np.where(data["Q22_rc"].isin(['Catholic', 'Evangelical Christian', 'Mainline Christian', 'Green or Russian Orthodox', "Jehovah's Witness", "Other Christian religion", "Mormon"]) , data["Q22_rc"], np.nan)
# recoding
for var in st.get_names(data, "Q43")[:-1]: #removing H which is not a CN Question
data[f'{var}_cat'] = data[var].cat.codes
#reverse the coding so higher score is higher CN score
data[f'{var}_cat'] = st.recode(data, f'{var}_cat', '4=NaN; 0=4;2=3; 3=2; 1=1')
for var in ['Q43_H', "Q43_F"]:
data[f'{var}_cat'] = data[var].cat.codes
#reverse the coding so higher score is higher CN score
data[f'{var}_cat'] = st.recode(data, f'{var}_cat', '4=NaN; 0=1;1=4;2=2; 3=3')
# Here are the counts
st.tabs(data, "Q22_rc")
# Here are the means
data[st.get_names(data, "Q43.+cat") + ['Q22_rc']] \
.groupby('Q22_rc') \
.apply(np.mean, axis = 1) \
.reset_index() \
.groupby('Q22_rc') \
.mean() \
.apply(lambda x: x.round(1)) \
.rename(columns = {0: 'CN_Score'}) \
.sort_values('CN_Score',ascending = False) \
.reset_index() \
.drop(columns = 'level_1') \
.to_clipboard()
# Mormons rank #3
data[data.Q22_rc.notna()] \
[st.get_names(data, "Q43.+cat")] \
.apply(np.mean, axis = 1) \
.mean()
data[data.Q22_rc.notna()].shape
# Average CN score of person of Christian in US
data[st.get_names(data, "Q43.+cat")] \
.apply(np.mean, axis = 1) \
.mean()
data.shape
# Average CN score of person in US
# What is their highest CN question?
data[st.get_names(data, "Q43.+cat") + ['Q22_rc']] \
.groupby('Q22_rc') \
.apply(np.mean) \
.reset_index()
# Finding where Mormons Deviate most from the average christian
data["mormon"] = np.where(data["Q22_rc"] == "Mormon", "Mormon", "Not Mormon")
data[data.Q22_rc.notna()] \
[st.get_names(data, "Q43.+cat") + ['mormon']] \
.groupby('mormon') \
.apply(np.mean) \
.apply(lambda x: float(x[0]) - float(x[1])) \
.sort_values(ascending = False)
# The question Mormons are most Christian Nationalist
data \
.pipe(lambda x: st.tabs(x, "Q22_rc","Q43_A", display="row")) \
.sort_values("Completely agree", ascending=False) \
[['Completely agree', 'Mostly agree', 'Mostly disagree', 'Completely disagree', 'Skipped/refused']] \
.round(0) \
.apply(lambda x: x/100) \
.to_clipboard()
# The question where Mormons are the least Christian Nationalist
data \
.pipe(lambda x: st.tabs(x, "Q22_rc","Q43_G", display="row")) \
.sort_values("Completely agree", ascending=False) \
[['Completely agree', 'Mostly agree', 'Mostly disagree', 'Completely disagree', 'Skipped/refused']] \
.round(0) \
.apply(lambda x: x/100) \
.to_clipboard()
# gist: https://gist.github.com/acbass49/a1c0b5e47ea10c9d4d59a6d0bbb16271
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment