How to read SPSS aka (.sav) in Python

Viewed 3087

It's my first time using Jupyter Notebook to analyze survey data (.sav file), and I would like to read it in a way it will show the metadata so I can connect the answers with the questions. I'm totally a newbie in this field, so any help is appreciated!

import pandas as pd
import pyreadstat
df, meta = pyreadstat.read_sav('./SimData/survey_1.sav')
type(df)
type(meta)
df.head()

Please lmk if there is an additional step needed for me to be able to see the metadata!

1 Answers

The meta object contains the metadata you are looking for. Probably the most useful attributes to look at are:

  • meta.column_names_to_labels : it's a dictionary with column names as you have in your pandas dataframe to labels meaning longer explanations on the meaning of each column
print(meta.column_names_to_labels)
  • meta.variable_value_labels : a dict where keys are column names and values are a dict where the keys are values you find in your dataframe and values are value labels.
print(meta.variable_value_labels)

For instance if you have a column "gender' with values 1 and 2, you could get: {"gender": {1:"male", 2:"female"}} which means value 1 is male and 2 female. You can get those labels from the beginning if you pass the argument apply_value_formats :

df, meta = pyreadstat.read_sav('survey.sav', apply_value_formats=True)

You can also apply those value formats to your dataframe anytime with pyreadstat.set_value_labels which returns a copy of your dataframe with labels:

df_copy = pyreadstat.set_value_labels(df, meta)
  • meta.missing_ranges : you get labels for missing values. Let's say in the survey in certain variable they encoded 1 meaning yes, 2 no and then mussing values, 5 meaning didn't answer, 6 person not at home. When you read the dataframe by default you will get values 1 and 2 and NaN (missing) instead of 5 and 6. You can pass the argument user_missing to get 5 and 6, and meta.missing_ranges will tell you that 5 and 6 are missing values. Variable_value_labels will give you the "didn't answer" and "person not at home" labels.
df, meta = pyreadstat.read_sav("survey.sav", user_missing=True)
print(meta.missing_ranges)
print(meta.variable_value_labels)

These are the potential pieces of information useful for your case, not necessarily all of these pieces will be present in your dataset.

More information here: https://ofajardo.github.io/pyreadstat_documentation/_build/html/index.html

Related