Pivot String column on Pyspark Dataframe
<p>I have a simple dataframe like this:</p>

<pre><code>rdd = sc.parallelize(
    [
        (0, "A", 223,"201603", "PORT"), 
        (0, "A", 22,"201602", "PORT"), 
        (0, "A", 422,"201601", "DOCK"), 
        (1,"B", 3213,"201602", "DOCK"), 
        (1,"B", 3213,"201601", "PORT"), 
        (2,"C", 2321,"201601", "DOCK")
    ]
)
df_data = sqlContext.createDataFrame(rdd, ["id","type", "cost", "date", "ship"])

df_data.show()
 +---+----+----+------+----+
| id|type|cost|  date|ship|
+---+----+----+------+----+
|  0|   A| 223|201603|PORT|
|  0|   A|  22|201602|PORT|
|  0|   A| 422|201601|DOCK|
|  1|   B|3213|201602|DOCK|
|  1|   B|3213|201601|PORT|
|  2|   C|2321|201601|DOCK|
+---+----+----+------+----+
</code></pre>

<p>and I need to pivot it by date:</p>

<pre><code>df_data.groupby(df_data.id, df_data.type).pivot("date").avg("cost").show()

+---+----+------+------+------+
| id|type|201601|201602|201603|
+---+----+------+------+------+
|  2|   C|2321.0|  null|  null|
|  0|   A| 422.0|  22.0| 223.0|
|  1|   B|3213.0|3213.0|  null|
+---+----+------+------+------+
</code></pre>

<p>Everything works as expected. But now I need to pivot it and get a non-numeric column:</p>

<pre><code>df_data.groupby(df_data.id, df_data.type).pivot("date").avg("ship").show()
</code></pre>

<p>and of course I would get an exception:</p>

<pre><code>AnalysisException: u'"ship" is not a numeric column. Aggregation function can only be applied on a numeric column.;'
</code></pre>

<p>I would like to generate something on the line of </p>

<pre><code>+---+----+------+------+------+
| id|type|201601|201602|201603|
+---+----+------+------+------+
|  2|   C|DOCK  |  null|  null|
|  0|   A| DOCK |  PORT| DOCK|
|  1|   B|DOCK  |PORT  |  null|
+---+----+------+------+------+
</code></pre>

<p>Is that possible with <code>pivot</code>?</p>