0

Experts, this is minor, but I am not able to just get it right.

+--------------+----------------------------------------------------------+-------------------+
|table         |query                                                     |date               |
+--------------+----------------------------------------------------------+-------------------+
|AGENT         |select * from table where DW_EFFECTIVE_DATE_PARTITION ='X'|2019-12-24 00:00:00|
+--------------+----------------------------------------------------------+-------------------+

All I want in this dataframe is to change column query to:

select * from table where DW_EFFECTIVE_DATE_PARTITION ='2019-12-24 00:00:00'

I tried:

>>> dfX.withColumn('query',regexp_replace('query',"'X'","'" + dfX['d'] + "'")).show()
Traceback (most recent call last):
TypeError: 'Column' object is not callable

Desired Output:

+--------------+----------------------------------------------------------------------------+-------------------+
|table         |query                                                                       |date             |
+--------------+----------------------------------------------------------------------------+-------------------+
|AGENT         |select * from table where DW_EFFECTIVE_DATE_PARTITION ='2019-12-24 00:00:00'|2019-12-24 00:00:00|
+--------------+----------------------------------------------------------------------------+-------------------+
0

3 Answers 3

2

You can use selectExpr instead of withColumn:

>>> df.selectExpr("table","regexp_replace(query, 'X', date) as query", "date").show(truncate=False)
+-----+----------------------------------------------------------------------------+-------------------+
|table|query                                                                       |date               |
+-----+----------------------------------------------------------------------------+-------------------+
|AGENT|select * from table where DW_EFFECTIVE_DATE_PARTITION ='2019-12-24 00:00:00'|2019-12-24 00:00:00|
+-----+----------------------------------------------------------------------------+-------------------+
Sign up to request clarification or add additional context in comments.

Comments

1

Use regexp_replace with expr so shat you could replace a string by another column value:

replace_expr = """regexp_replace(query,"'X'",concat("'", date, "'"))"""
df.withColumn("query", expr(replace_expr)).show(truncate=False)

Gives:

+-----+----------------------------------------------------------------------------+-------------------+
|table|query                                                                       |date               |
+-----+----------------------------------------------------------------------------+-------------------+
|AGENT|select * from table where DW_EFFECTIVE_DATE_PARTITION ='2019-12-24 00:00:00'|2019-12-24 00:00:00|
+-----+----------------------------------------------------------------------------+-------------------+

Comments

0
def replace_string(s):
  if s == "A":
    return "a"
  else:
    return "b"
replace_string_udf = spark.udf.register("replace_string", replace_string, StringType())
df = df.withColumn("new_column", replace_string_udf("old_column_name"))

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.