3

I'm using pyspark 2.0 I have a df like this:

    +----------+----------+--------
    |pid       |      date| p_category
    +----------+----------+--------
    |    1ba   |2016-09-30|flat
    |    3ed   |2016-09-30|ultra_thin
    +----------+----------+----------

I did a

    df.groupBy("p_category","date") \                        
    .agg(countDistinct("pid").alias('cnt'))

and I got this:

    +-------------+----------+------+
    |p_category   |      date|   cnt|
    +-------------+----------+------+
    |    flat     |2016-09-30|116251|
    |ultra_thin   |2016-09-30|113017|
    +-------------+----------+------+

But I want I pivot table like this:

    +----------+----------+------+
    |date      |      flat|  ultra-thin
    +----------+----------+------+
   2016-09-30  |    116251|113017
    ------------------------------
   df.groupBy("p_category","date") \                        
    .agg(countDistinct("pid").alias('cnt')).pivot("p_category")

I got this error:

'DataFrame' object has no attribute 'pivot'

How could I do a pivot in such case or is there other solution? Thanks

1 Answer 1

5

You should call pivot on gruped data, so first you need to group by date and then pivot by p_category:

>>> df.groupBy('date').pivot('p_category').agg(countDistinct('pid').alias('cnt')).show()
+----------+----+----------+                                                    
|      date|flat|ultra_thin|
+----------+----+----------+
|2016-09-30|   1|         1|
+----------+----+----------+
Sign up to request clarification or add additional context in comments.

1 Comment

Looks like a link is outdated but it can still be found on spark.apache.org/docs

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.