Skip to content

Instantly share code, notes, and snippets.

@alexpetralia
Created September 25, 2016 02:37
Show Gist options
  • Save alexpetralia/2cbfe0a8c4bfc42580987f5ef6184ca3 to your computer and use it in GitHub Desktop.
Save alexpetralia/2cbfe0a8c4bfc42580987f5ef6184ca3 to your computer and use it in GitHub Desktop.
# Correct response types
df.loc[df['response'].str.contains(r'did not', case=False) | df['response'].str.contains(r'---'), 'response'] = np.nan
# Convert empty string comments into null types
df['comment'] = df['comment'].replace(r'^$', np.nan, regex=True)
# Assign sex variable to economists
sex = pd.read_csv(os.path.join(os.path.dirname(__file__), 'economist_sex_mapping.csv'), index_col='economist_name')
df['sex'] = df['economist_name'].map(sex['sex'])
# Assign response categories to numerical values
certainty_mapping = {
'Strongly Disagree': -2,
'Disagree': -1,
'Uncertain': 0,
'No opinion': 0,
'Agree': 1,
'Strongly Agree': 2,
}
df = df.assign(response_int = lambda x: x['response'].map(certainty_mapping))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment