PySpark Dataframe identify distinct value on one column based on duplicate values in other columns
<p>I have a pyspark dataframe like: where c1,c2,c3,c4,c5,c6 are the columns</p>

<blockquote>
<pre><code>   +----------------------------+   
   |c1 | c2 | c3 | c4 | c5 | c6 |  
   |----------------------------|   
   | a |  x |  y |  z |  g |  h |    
   | b |  m |  f |  l |  n |  o |    
   | c |  x |  y |  z |  g |  h |    
   | d |  m |  f |  l |  n |  o |    
   | e |  x |  y |  z |  g |  i |   
   +----------------------------+
</code></pre>
</blockquote>

<p>I want to extract c1 values for the rows which have same c2,c3,c4,c5 values but different c1 values.
Like, 1st, 3rd &amp; 5th rows have same values for c2,c3,c4 &amp; c5 but different c1 value. So the output should be <strong>a, c &amp; e</strong>.<br>
<strong>(update)</strong>
similarly, 2nd &amp; 4th rows have same values for c2,c3,c4 &amp; c5 but different c1 value. So the output should also contain <strong>b &amp; d</strong>  </p>

<p>How can I obtain such result ? I have tried applying groupby but I don't understand how to obtain distinct values for c1. </p>

<p><strong>UPDATE:</strong>  </p>

<p>Output should be a Dataframe of c1 values  </p>

<pre><code># +-------+
# |c1_dups|
# +-------+
# |  a,c,e|
# |    b,e|
# +-------+   
</code></pre>

<p><strong>My Approach:</strong>  </p>

<pre><code>m = data.groupBy('c2','c3','c4','c5)
</code></pre>

<p>but I'm not understanding how to retrieve the values in m. I'm new to pyspark dataframes hence very much confused</p>

<p>This is actually very simple, let's create some data first :</p>

<pre><code>schema = ['c1','c2','c3','c4','c5','c6']

rdd = sc.parallelize(["a,x,y,z,g,h","b,x,y,z,l,h","c,x,y,z,g,h","d,x,f,y,g,i","e,x,y,z,g,i"]) \
        .map(lambda x : x.split(","))

df = sqlContext.createDataFrame(rdd,schema)
# +---+---+---+---+---+---+
# | c1| c2| c3| c4| c5| c6|
# +---+---+---+---+---+---+
# |  a|  x|  y|  z|  g|  h|
# |  b|  x|  y|  z|  l|  h|
# |  c|  x|  y|  z|  g|  h|
# |  d|  x|  f|  y|  g|  i|
# |  e|  x|  y|  z|  g|  i|
# +---+---+---+---+---+---+
</code></pre>

<p>Now the fun part, you'll just need to import some functions, group by and explode as following :</p>

<pre><code>from pyspark.sql.functions import *

dupes = df.groupBy('c2','c3','c4','c5') \ 
          .agg(collect_list('c1').alias("c1s"),count('c1').alias("count")) \ # we collect as list and count at the same time
          .filter(col('count') &gt; 1) # we filter dupes

df2 = dupes.select(explode("c1s").alias("c1_dups"))

df2.show()
# +-------+
# |c1_dups|
# +-------+
# |      a|
# |      c|
# |      e|
# +-------+
</code></pre>

<p>I hope this answers your question.</p>

python apache-spark dataframe pyspark