HyukjinKwon commented on a change in pull request #23414: [SPARK-26449][PYTHON] 
add a transform method to the Dataframe class
URL: https://github.com/apache/spark/pull/23414#discussion_r244655655
 
 

 ##########
 File path: python/pyspark/sql/dataframe.py
 ##########
 @@ -2046,6 +2046,40 @@ def toDF(self, *cols):
         jdf = self._jdf.toDF(self._jseq(cols))
         return DataFrame(jdf, self.sql_ctx)
 
+    @since(3.0)
+    def transform(self, func):
+        """Returns a new class:`DataFrame` according to a user-defined custom 
transform method.
+        This allows chaining transformations rather than using nested or 
temporary variables.
+
+        :param func: a user-defined custom transform function
+        This is equiavalent to a nested call:
+            actual_df = with_something(with_greeting(source_df), "crazy"))
+
+        credit to: 
https://medium.com/@mrpowers/chaining-custom-pyspark-transformations-4f38a8c7ae55
+
+        A more concrete example::
+        >>> sc = pyspark.SparkContext(master='local')
+        >>> spark = pyspark.sql.SparkSession(sparkContext=sc)
+        >>> from pyspark.sql.functions import lit
+        >>> def with_greeting(df):
+        ...     return df.withColumn("greeting", lit("hi"))
+        >>> def with_something(df, something):
+        ...     return df.withColumn("something", lit(something))
+        >>> data = [("jose", 1), ("li", 2), ("liz", 3)]
+        >>> source_df = spark.createDataFrame(data, ["name", "age"])
+        >>> actual_df = source_df.transform(with_greeting).transform(lambda x: 
with_something(x, "crazy"))
 
 Review comment:
   I think we don't necessarily have to demonstrate the chaining of multiple 
`transform`. We can chain other APIs as well, for instance, 
`df.transform(...).select(...).transform(...)` in that sense.
   
   `show()` is already DataFrame API. I think `df.transform(...).show()` is 
simple and good enough.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to