0

I have a pyspark dataframe, with text column.

  1. I wanted to map the values which with a regex expression.
    df = df.withColumn('mapped_col', regexp_replace('mapped_col', '.*-RH', 'RH'))
    df = df.withColumn('mapped_col', regexp_replace('mapped_col', '.*-FI, 'FI'))
  1. Plus I wanted to map specifics values according to a dictionnary, I did the following (mapper is from create_map()):
     df = df.withColumn("mapped_col",mapper.getItem(F.col("action")))
  1. Finaly the values which has not been mapped by the dictionnary or the regex expression, will be set null. I do not know how to do this part in accordance to the two others.

Is it possible to have like a dictionnary of regex expression so I can regroup the two 'functions'? {".*-RH": "RH", ".*FI" : "FI"}

Original Output Example

+-----------------------------+
|message                      |
+-----------------------------+
|GDF2009                      | 
|GDF2014                      |
|ADS-set                      |
|ADS-set                      |
|XSQXQXQSDZADAA5454546a45a4-FI|
|dadaccpjpifjpsjfefspolamml-FI|
|dqdazdaapijiejoajojp565656-RH|
|kijipiadoa
+-----------------------------+

Expected Output Example

+-----------------------------+-----------------------------+
|message                      |status|
+-----------------------------+-----------------------------+
|GDF2009                      | GDF
|GDF2014                      | GDF
|ADS/set                      | ADS
|ADS-set                      | ADS 
|XSQXQXQSDZADAA5454546a45a4-FI| FI
|dadaccpjpifjpsjfefspolamml-FI| FI
|dqdazdaapijiejoajojp565656-RH| RH
|kijipiadoa                   | null or ??

So first 4th line are mapped with a dict, and the other are mapped using regex. Unmapped are null or ?? Thank you,

2
  • Could you add input and expected output dataframe? Commented Jul 7, 2020 at 11:05
  • I have edited my post, I hope it will help you Commented Jul 7, 2020 at 11:22

1 Answer 1

1

You can achieve it using contains function:

from pyspark.sql.types import StringType

df = spark.createDataFrame(
    ["GDF2009", "GDF2014", "ADS-set", "ADS-set", "XSQXQXQSDZADAA5454546a45a4-FI", "dadaccpjpifjpsjfefspolamml-FI",
     "dqdazdaapijiejoajojp565656-RH", "kijipiadoa"], StringType()).toDF("message")
df.show()

names = ("GDF", "ADS", "FI", "RH")

def c(col, names):
    return [f.when(f.col(col).contains(i), i).otherwise("") for i in names]

df.select("message", f.concat_ws("", f.array_remove(f.array(*c("message", names)), "")).alias("status")).show()

output:

+--------------------+
|             message|
+--------------------+
|             GDF2009|
|             GDF2014|
|             ADS-set|
|             ADS-set|
|XSQXQXQSDZADAA545...|
|dadaccpjpifjpsjfe...|
|dqdazdaapijiejoaj...|
|          kijipiadoa|
+--------------------+

+--------------------+------+
|             message|status|
+--------------------+------+
|             GDF2009|   GDF|
|             GDF2014|   GDF|
|             ADS-set|   ADS|
|             ADS-set|   ADS|
|XSQXQXQSDZADAA545...|    FI|
|dadaccpjpifjpsjfe...|    FI|
|dqdazdaapijiejoaj...|    RH|
|          kijipiadoa|      |
+--------------------+------+
Sign up to request clarification or add additional context in comments.

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.