[英]How to read SPSS aka (.sav) in Python
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.这是我第一次使用 Jupyter Notebook 分析调查数据(.sav 文件),我想以一种显示元数据的方式阅读它,以便我可以将答案与问题联系起来。 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!如果我需要额外的步骤才能看到元数据,请 lmk!
The meta object contains the metadata you are looking for. meta object 包含您要查找的元数据。 Probably the most useful attributes to look at are:
最有用的属性可能是:
print(meta.column_names_to_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:例如,如果你有一个值为 1 和 2 的“gender”列,你可以得到:{"gender": {1:"male", 2:"female"}} 这意味着值 1 是男性,2 是女性。你如果传递参数 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:您还可以随时使用 pyreadstat.set_value_labels 将这些值格式应用于您的 dataframe,它会返回带有标签的 dataframe 的副本:
df_copy = pyreadstat.set_value_labels(df, meta)
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更多信息在这里: https://ofajardo.github.io/pyreadstat_documentation/_build/html/index.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.