File tree Expand file tree Collapse file tree 1 file changed +21
-1
lines changed Expand file tree Collapse file tree 1 file changed +21
-1
lines changed Original file line number Diff line number Diff line change 3030from pyspark .sql .dataframe import Column , _to_java_column , _to_seq
3131
3232
33- __all__ = ['countDistinct' , 'approxCountDistinct' , 'udf' ]
33+ __all__ = ['countDistinct' , 'approxCountDistinct' , 'udf' , 'coalesce' ]
3434
3535
3636def _create_function (name , doc = "" ):
@@ -75,6 +75,26 @@ def _(col):
7575__all__ .sort ()
7676
7777
78+ def coalesce (* cols ):
79+ """Returns the first column that is not null.
80+
81+ >>> df.select(coalesce(df["a"], df["b"])).show()
82+ Coalesce(a,b)
83+ 1
84+ 3
85+ 5
86+
87+ >>> df.select('*', coalesce(df["a"], lit(0.0))).show()
88+ a b Coalesce(a,0.0)
89+ 1 2 1.0
90+ null 3 0.0
91+ 5 null 5.0
92+ """
93+ sc = SparkContext ._active_spark_context
94+ jc = sc ._jvm .functions .coalesce (_to_seq (sc , cols , _to_java_column ))
95+ return Column (jc )
96+
97+
7898def countDistinct (col , * cols ):
7999 """Returns a new :class:`Column` for distinct count of ``col`` or ``cols``.
80100
You can’t perform that action at this time.
0 commit comments