Determine column value based on 2 other columns
<p>There are 2 column, Label1 and Label2. Both of them are cluster labels using different methods. </p>

<pre><code>  Label1 Label2
0   0    1024
1   1    1024
2   2    1025
3   3    1026
4   3    1027
5   4    1028
</code></pre>

<p>I wanna get the final cluster label based these 2 columns. Compared each row, as long as one of these two labels are same, they are in the same cluster.</p>

<p>For example: row 0 and row 1 have label 2 in common, row 3 and row 4 have label1 in common, thus row0 and row1 in the same group and row3 and row4 in the same group.  So the results I'd like to have:</p>

<pre><code>   Label1 Label2 Cluster ID
0   0    1024    0
1   1    1024    0
2   2    1025    1
3   3    1026    2
4   3    1027    2
5   4    1028    3
</code></pre>

<p>What's the best way to do thisï¼
Any help would be appreciated.</p>

<p>Edited: I think I didn't give a good example. Acutally,  labels are not necessarily in any order:</p>

<pre><code>  Label1 Label2
0   0    1024
1   1    1023
2   2    1025
3   3    1024
4   3    1027
5   4    1022
</code></pre>

<p>Try this: Use np.where and <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.duplicated.html" rel="nofollow">pandas.duplicated</a></p>

<pre><code>df             = df.sort_values(['Label1', 'Label2'])
df['Cluster']  = np.where( (df.Label1.duplicated()) | (df.Label2.duplicated()),0,1).cumsum()
print df

       Label1  Label2  Cluster
0       0    1024        1
1       1    1024        1
2       2    1025        2
3       3    1026        3
4       3    1027        3
5       4    1028        4
</code></pre>

python pandas dataframe