1

I want to check if last two values of the array in PySpark Dataframe is [1, 0] and update it to [1, 1]

Input Dataframe

Column1    Array_column
abc        [0,1,1,0]
def        [1,1,0,0]
adf        [0,0,1,0]

Output Dataframe

Column1    Array_column
abc        [0,1,1,1]
def        [1,1,0,0]
adf        [0,0,1,1]

3 Answers 3

3
 >>> def udf1(i):
      if (i[2]==1) & (i[3]==0):
       i[3]=1
      else:
        i[3]=i[3]
      return i

>>> udf2=udf(udf1)
df1.withColumn("Array_Column",udf2(col("Array_Column"))).show()



+-------+------------+
|Column1|Array_Column|
+-------+------------+
|    abc|[0, 1, 1, 1]|
|    def|[1, 1, 0, 0]|
|    adf|[0, 0, 1, 1]|
+-------+------------+
Sign up to request clarification or add additional context in comments.

Comments

1

You can combine array functions with when expression :

from pyspark.sql import functions as F

df1 = df.withColumn(
    "Array_column",
    F.when(
        F.slice("Array_column", -2, 2) == F.array(F.lit(1), F.lit(0)),
        F.flatten(F.array(F.expr("slice(Array_column, 1, size(Array_column) - 2)"), F.array(F.lit(1), F.lit(1))))
    ).otherwise(F.col("Array_column"))
)

df1.show()

#+-------+------------+
#|Column1|Array_column|
#+-------+------------+
#|    abc|[0, 1, 1, 1]|
#|    def|[1, 1, 0, 0]|
#|    adf|[0, 0, 1, 1]|
#+-------+------------+

Comments

1

You can slice the array, do a case when for the last two elements, and combine the two slices using concat.

import pyspark.sql.functions as F

df2 = df.withColumn(
    'Array_column',
    F.expr("""
        concat(
            slice(Array_column, 1, size(Array_column) - 2),
            case when slice(Array_column, size(Array_column) - 1, 2) = array(1,0) 
                 then array(1,1)
                 else slice(Array_column, size(Array_column) - 1, 2)
            end
         )
    """)
)

df2.show()
+-------+------------+
|Column1|Array_column|
+-------+------------+
|    abc|[0, 1, 1, 1]|
|    def|[1, 1, 0, 0]|
|    adf|[0, 0, 1, 1]|
+-------+------------+

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.