title: Groupby with when condition in Pyspark
body: <p>My data frame looks like</p>
  
  <pre><code>    id      |reg_date  |      txn_date|
  +----------+----------+--------------------+
  |1          |2019-01-06| 2019-02-15 12:51:15|
  |1          |2019-01-06| 2019-03-29 13:15:27|
  |1          |2019-01-06| 2019-06-01 01:42:57|
  |1          |2019-01-06| 2019-01-06 17:01:...|
  |5          |2019-06-16| 2019-07-19 11:50:34|
  |5          |2019-06-16| 2019-07-13 19:49:39|
  |5          |2019-06-16| 2019-08-27 17:37:22|
  |2          |2018-07-30| 2019-01-01 07:03:...|
  |2          |2018-07-30| 2019-07-30 01:27:57|
  |2          |2018-07-30| 2019-02-01 00:08:35
  </code></pre>
  
  <p>I want to pickup the 1st <code>txn_date</code> after <code>reg_date</code> , i.e. the first <code>txn_date</code> of <code>reg_date &gt;= txn_date</code>.</p>
  
  <p>Expected output </p>
  
  <pre><code>    id      |reg_date  |      txn_date|
  +----------+----------+--------------------+
  |1          |2019-01-06| 2019-01-06 17:01:...|
  |5          |2019-06-16| 2019-07-13 19:49:39|
  |2          |2018-07-30| 2019-07-30 01:27:57|
  </code></pre>
  
  <p>I have done so far,</p>
  
  <pre><code>df = df.withColumn('txn_date',to_date(unix_timestamp(F.col('txn_date'),'yyyy-MM-dd HH:mm:ss').cast("timestamp")))
  
  df = df.withColumn('reg_date',to_date(unix_timestamp(F.col('reg_date'),'yyyy-MM-dd').cast("timestamp")))
  
  gg = df.groupBy('id','reg_date').agg(min(F.col('txn_date')))
  </code></pre>
  
  <p>But getting wrong results.</p>