我正在尝试将用sas编写的if语句转换为pyspark;下面是代码: Query here是它们直接提到列名并执行if函数。但是,如果我们使用pyspark,我们将使用它dfcolname。有很多if语句;它们有没有办法在单个语法/语句中涵盖这一点。
if S1= "" and S2= "" then
do
S1= S0- S3;
S2= 0;
end;
if CS1= "" and CS2= "" then
do
CS1= CS0 - CS3;
CS2= 0;
end;
if S1< 0 then
S1= 0;
if S2< 0 then
S2= 0;
if CS1< 0 then
CS1= 0;
if CS2< 0 then
CS2= 0;
if IMP1= "" then
IMP1= 0;
if IMP2= "" then
IMP2= 0;
if CIMP1 = "" then
CIMP1 = 0;
if CIMP2 = "" then
CIMP2 = 0;发布于 2021-01-07 03:42:43
我不太明白你的问题。据我所知,我们也可以在pyspark中做同样的事情(“他们直接提到列名并执行if函数”)。下面是转换后的示例代码。它是由一个名为链轮的自动化工具生成的,有了它,数百万这样的“如果”条件可以在一分钟内转换。
.withColumn('S1', expr("""case
when (S1= '' and S2= '') then S0- S3
else S1 end"""))
.withColumn('S2', expr("""case
when (S1= '' and S2= '') then 0
else S2 end"""))
.withColumn('CS1', expr("""case
when (CS1= '' and CS2= '') then CS0 - CS3
else CS1 end"""))
.withColumn('CS2', expr("""case
when (CS1= '' and CS2= '') then 0
else CS2 end"""))
.withColumn('S1', expr("""case
when (S1< 0) then 0
else S1 end"""))
.withColumn('S2', expr("""case
when (S2< 0) then 0
else S2 end"""))
.withColumn('CS1', expr("""case
when (CS1< 0) then 0
else CS1 end"""))
.withColumn('CS2', expr("""case
when (CS2< 0) then 0
else CS2 end"""))
.withColumn('IMP1', expr("""case
when (IMP1= '') then 0
else IMP1 end"""))
.withColumn('IMP2', expr("""case
when (IMP2= '') then 0
else IMP2 end"""))
.withColumn('CIMP1', expr("""case
when (CIMP1 = '') then 0
else CIMP1 end"""))
.withColumn('CIMP2', expr("""case
when (CIMP2 = '') then 0
else CIMP2 end"""))https://stackoverflow.com/questions/61786015
复制相似问题