title: How to get unique values of a column in pyspark dataframe and store as new column
body: <p>Basically I want to know how much a brand that certain customer buy in other dataset and rename it as change brand, here's what I did in Pandas</p>
  <pre><code>firstvalue=firstvalue.merge((pd.DataFrame(profile.groupby('msisdn')
                                            .handset_brand.nunique()
                                            .rename('hpbrand_change_num'))
                                            .reset_index()),how='left',on=['msisdn'])
  </code></pre>
  <p>Here's what I did (without merge) in pyspark</p>
  <pre><code>fd_subsprofile.groupBy(&quot;msisdn&quot;)\
                .handset_brand.nunique()\
                .withColumn('hpbrand_change_num')\
                .reset_index()
  </code></pre>
  <p>The error message</p>
  <pre><code>AttributeError: 'GroupedData' object has no attribute 'handset_brand'
  </code></pre>
  <p>Then, I try</p>
  <pre><code>fd_subsprofile.groupBy(&quot;msisdn&quot;).select(&quot;handset_brand&quot;).count().show()
  </code></pre>
  <p>The error message</p>
  <pre><code>AttributeError: 'GroupedData' object has no attribute 'select'
  </code></pre>
  <p>How to this in pyspark?</p>