import pandas as pd
# Load the data
survey = pd.read_spss("data/mm_survey_recoded-all-main_SPSS.sav")
# Renaming columns (including the new columns from the R code)
survey.rename(columns={
'Q5.2': 'motivations',
'Q5.1': 'avenues_txt',
'Q5.3': 'outlook',
'Q5.4': 'outlook_txt',
'Q6.1': 'age',
'Q6.2': 'gender',
'Q6.3': 'sexuality',
'R6.4': 'ethnicity',
'Q6.5': 'disability',
'R2.25': 'technology',
'Q7.2': 'website',
'Q2.13': 'online_teaching',
'Q2.14': 'online_n',
'Q2.10': 'year_started',
'R6.6': 'formal_education',
'Q6.7': 'prof_qual',
'R6.7_3': 'profession',
'Q3.7': 'supervise_emp',
'Q3.5': 'employment_other_type',
'Q3.6': 'job_title',
'Q2.23': 'trained',
'Q2.24': 'trained_n',
'R2.6_1': 'advocacy',
'R2.6_2': 'business_entrepreneurial',
'R2.6_3': 'management',
'R2.11': 'courses_in_year',
'Q2.12': 'clients_taught',
'Q2.15': 'independent',
'R2.16': 'nations',
'R2.17': 'region_england',
'R2.18': 'region_wales',
'R2.19': 'region_scotland',
'R.2.20': 'region_ireland',
'R2.22': 'area',
'R.2.21': 'contexts'
}, inplace=True)
category_columns = [
'age',
'gender',
'sexuality',
'ethnicity',
'disability',
'technology',
'website',
'online_teaching',
'online_n',
'formal_education',
'prof_qual',
'profession',
'supervise_emp',
'employment_other_type',
'trained',
'trained_n',
'advocacy',
'business_entrepreneurial',
'management',
'courses_in_year',
'clients_taught',
'independent',
'nations',
'region_england',
'region_wales',
'region_scotland',
'region_ireland',
'area',
'contexts',
]
for col in category_columns:
if col in survey.columns:
survey[col] = survey[col].astype('category')
else:
print(f"Column {col} not found in DataFrame.")
# Convert 'job_title' to string
survey['job_title'] = survey['job_title'].astype(str)
# Replace empty strings with NaN and convert to float
survey['year_started'] = survey['year_started'].replace('', np.nan).astype(float)
# Convert the entire series to nullable integer type Int64
survey['year_started'] = survey['year_started'].astype('Int64')
# Print some values of 'year_started' to check the transformation
#print(survey['year_started'].head())
# Save the modified DataFrame
survey.to_pickle('data/tidied_survey.pkl')
# Print number of users
#print(f"Total number of users after transform: {survey.shape[0]}")