简体   繁体   English

我希望添加一列并根据条件在每一行中更新它

[英]I wish to add a column and update it in each row depending on a condition

Input Dataset输入数据集

For the above dataset, write a python or SQL command such that it returns the following对于上述数据集,编写 python 或 SQL 命令,使其返回以下内容

For each set of assignmentID, if the correct value is 1, append a new column with value 1 In case there are multiple rows with same assignmentid, then put 1 in new column only when there atleast 3 1's or at the last.对于每组assignmentID,如果正确的值为1,append 新列值为1 如果有多行具有相同的assignmentid,则仅当至少有3个1或最后一个1时才将1放入新列。 Expected Output (First Column is only for ref to represent that there are rows with multiple assigment ID预期 Output (第一列仅用于 ref 表示存在具有多个分配 ID 的行

My logic was我的逻辑是

> a = [0,1,1,1,0,1,0,1] counter = 0 for i in a: 
>     if (a[i] == a[i+1]):
>       counter=counter+1
>     else:
>       counter=0 
>     if (counter>=3):
>       break if (counter==3):   print("Mastered") else:   print("WheelSpinning")

But I am not sure how to run this through the dataset or write it as SQL for above data.但我不确定如何通过数据集运行它或将其写为 SQL 以获取上述数据。

If you wish to use python, Then here is an example, Take the last index in each row and append an element that saitisfies your conditions while iterating through every row of your dataset.如果您希望使用 python,那么这里是一个示例,取每行中的最后一个索引和 append 一个满足您的条件的元素,同时遍历数据集的每一行。

You could use sequel to retrieve all the data: SELECT * FROM TABLE_NAME您可以使用 sequel 检索所有数据: SELECT * FROM TABLE_NAME

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

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