python - 使用 SPSS Python 脚本获取列值

标签 python scripting spss

我有一个 SPSS Python 脚本,它循环访问表并读取特定列的每一行值,并相应地设置参数值。目前,我正在使用 getValueAt(rowIndex, colIndex) 访问值。但是,必须引用列索引(而不是列名称)并不理想,因为表列可能会发生变化。有没有办法根据列名引用值?

示例代码

diagram = modeler.script.diagram()

for i in range(nrows):

    jobid = job_rowset.getValueAt(i, 0);
    city = job_rowset.getValueAt(i, 3);
    country = job_rowset.getValueAt(i, 4);

    diagram.setParameterValue ('BU', bu)
    diagram.setParameterValue ('City_Param', city)
    diagram.setParameterValue ('CountryID_Param', country)

感谢任何帮助!谢谢!

最佳答案

假设第一行将包含名称(或者您有指定列顺序的可用内容),您可以使用列名称作为键和列编号值来构造一个字典。

这会给出类似的内容:

name_key_dict = dict()
for i in range(ncols):
    name_key_dict[colnames[i]] = i # Assuming that names is the ordered list of columns
# I would add a check here that you have the required columns

param_col_list = [ # This constructs a list of parameters vs column numbers
    ('BU', name_key_dict.get('Bu_Col')),
    ('City_Param', name_key_dict.get('City')), 
    ('CountryID_Param', name_key_dict.get('Country Code')), 
]
for row in job_rowset: # Assuming job_rowset is an iterable with a member of getValue
    for (param, col) in param_col_list:
        diagram.setParameterValue(param, row.getValue(col))

关于python - 使用 SPSS Python 脚本获取列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26751986/

相关文章:

batch-file - 打开可执行文件并捕捉到桌面左上角的脚本

python - 如何使用 .SAS 或 SPS 元数据文件将 CSV 读取为 Pandas 数据帧?

python - 如何对时间序列数据进行分组

python - Python中的模块和脚本有什么区别?

python - 如何通过CMD安装python 2.7

loops - 使用引用变量在 SPSS 中赋值

statistics - SPSS : syntax error involving RECODE command

python - 使用 python 字典进行计数并添加功能

python - 为什么会出现此错误 "django.db.utils.OperationalError: (1050, "表 'someTable' 已存在")"

python - 许多小请求与少量大请求 - Angular 到 Django REST API - 不涉及数据库