我有一个 Dataframe df,如下所示:
VehNum Control_circuit control_circuit_status partnumbers errors Flag
4234456 DOC ok A567UR Software Issue 0
4234456 DOC not_okay A568UR Software Issue 1
4234456 DOC not_okay A569UR Hardware issue 2
4234457 ACR ok A234TY Hardware issue 0
4234457 ACR ok A235TY Hardware issue 0
4234457 ACR ok A234TY Hardware issue 0
4234487 QWR ok A276TY Hardware issue 0
4234487 QWR not_okay A872UR Hardware issue 1
3423448 QWR not_okay A872UR Hardware issue 1
我想添加一个名为“Control_Flag”的新列,并执行以下操作:对于每个VehNum,Control_circuit,如果其标志值仅为0,则Control_Flag列将保持值0,否则如果其标志值为0,1或2,则Control_Flag列将保持值1
结果如下:
VehNum Control_circuit control_circuit_status partnumbers errors Flag Control_Flag
4234456 DOC ok A567UR Software Issue 0 1
4234456 DOC not_okay A568UR Software Issue 1 1
4234456 DOC not_okay A569UR Hardware issue 2 1
4234457 ACR ok A234TY Hardware issue 0 0
4234457 ACR ok A235TY Hardware issue 0 0
4234457 ACR ok A234TY Hardware issue 0 0
4234487 QWR ok A276TY Hardware issue 0 1
4234487 QWR not_okay A872UR Hardware issue 1 1
3423448 QWR not_okay A872UR Hardware issue 1 1
如何使用pyspark实现这一点?
1条答案
按热度按时间fcy6dtqo1#
使用带有SUM()的聚合窗口将有助于实现这一点
输出: