简体   繁体   English

尝试遍历 Python 中的不同调查

[英]Trying to loop through different surveys in Python

I'm trying to loop through different sections in my survey in python to calculate the total time taken to answer each specific section (there are three sections: Warehouse, psychometrics and healthcare).我正在尝试遍历 python 调查中的不同部分,以计算回答每个特定部分所需的总时间(共有三个部分:仓库、心理测量和医疗保健)。

My data is in a csv format and for each section in the survey, time data is displayed in 9 columns that start with a common suffix (ie, 'sectionname'.timedata).我的数据采用 csv 格式,对于调查中的每个部分,时间数据显示在以通用后缀(即“sectionname”.timedata)开头的 9 列中。 So to calculate the time spent in each section, I wrote a line of code which sums up all the values in those columns for the specific section:因此,为了计算每个部分花费的时间,我编写了一行代码来总结特定部分的这些列中的所有值:

Surveyresp['Warehouse_time'] = Surveyresp[[col for col in Surveyresp.columns if col.startswith('Warehouse.timedata')]].sum(axis=1)

Surveyresp['Psychometrics_time'] = Surveyresp[[col for col in Surveyresp.columns if col.startswith('Psychometric.timedata')]].sum(axis=1)

Surveyresp['Healthcare_time'] = Surveyresp[[col for col in Surveyresp.columns if col.startswith('Healthcare.timedata')]].sum(axis=1)

My question is, is there a way I can just loop through these 3 surveys to make this change?我的问题是,有没有一种方法可以让我循环通过这 3 个调查来做出改变?

I tried starting the loop:我尝试启动循环:

Surveys = ['Warehouse.timedata', 'Psychometric.timedata']

for i in Surveys:
   print(i)
   Surveyresp['i_time'] = Surveyresp[[col for col in Surveyresp.columns if col.startswith('i')]].sum(axis=1)

But all this loop does is create one new variable (that is, 'i_time').但是这个循环所做的只是创建一个新变量(即“i_time”)。 What am I doing wrong with this loop?我在这个循环中做错了什么?

Thanks!谢谢!

Your idea of the loop was almost correct.您对循环的想法几乎是正确的。 :) :)

Surveys = ['Warehouse.timedata', 'Psychometric.timedata']

for i in Surveys:
   print(i)
   collum_name = i.split(".")
   Surveyresp[] = Surveyresp[[col for col in Surveyresp.columns if col.startswith('i')]].sum(axis=1)

But it should look like this to correctly loop over all columns但它应该看起来像这样正确循环所有列

Surveys = ['Warehouse', 'Psychometric']

for i in Surveys:
    print(i)
    Surveyresp[i+"_time"] = Surveyresp[[col for col in Surveyresp.columns if col.startswith(i+".timedata")]].sum(axis=1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM