create a bigram from a column in pandas df
<p>i have this test table in pandas dataframe</p>

<pre><code>   Leaf_category_id  session_id  product_id
0               111           1         987
3               111           4         987
4               111           1         741
1               222           2         654
2               333           3         321
</code></pre>

<p>this is the extension of my previous question, which was answered by @jazrael.
<a href="https://stackoverflow.com/questions/40587902/function-to-select-from-columns-pandas-df/40587934#40587934">view answer</a></p>

<p>so after getting the values in product_id column as(just an assumption, little different from the output of my previous question,</p>

<pre><code>   |product_id               |
   ---------------------------
   |111,987,741,34,12        |
   |987,1232                 |
   |654,12,324,465,342,324   |
   |321,741,987              |
   |324,654,862,467,243,754  |
   |6453,123,987,741,34,12   |
</code></pre>

<p>and so on,
i want to create a new column, in which all the values in a row should be made as a bigram with its next one, and the last no. in the row combined with the first one,for example:</p>

<pre><code>   |product_id               |Bigram
   -------------------------------------------------------------------------
   |111,987,741,34,12        |(111,987),**(987,741)**,(741,34),(34,12),(12,111)
   |987,1232                 |(987,1232),(1232,987)
   |654,12,324,465,342,32    |(654,12),(12,324),(324,465),(465,342),(342,32),(32,654)
   |321,741,987              |(321,741),**(741,987)**,(987,321)
   |324,654,862              |(324,654),(654,862),(862,324)
   |123,987,741,34,12        |(123,987),(987,741),(34,12),(12,123)
</code></pre>

<p>ignore the **( i'll tell you later on why i starred that)</p>

<p>the code to achive the bigram is</p>

<pre><code>for i in df.Leaf_category_id.unique(): 
    print (df[df.Leaf_category_id == i].groupby('session_id')['product_id'].apply(lambda x: list(zip(x, x[1:]))).reset_index())
</code></pre>

<p>from this df, i want to consider the bigram column and make one more column named as frequency, which gives me frequency of bigram occured.</p>

<blockquote>
  <p>Note* : (987,741) and (741,987) are to be considered as same and one dublicate entry should be removed and thus frequency of  (987,741) should be 2.
  similar is the case with (34,12) it occurs two times, so frequency should be 2</p>
</blockquote>

<pre><code>   |Bigram
   ---------------
   |(111,987),
   |**(987,741)**
   |(741,34)
   |(34,12)
   |(12,111)
   |**(741,987)**
   |(987,321)
   |(34,12)
   |(12,123)
</code></pre>

<p>Final Result should be.</p>

<pre><code>   |Bigram       | frequency |
   --------------------------
   |(111,987)    |  1 
   |(987,741)    |  2
   |(741,34)     |  1
   |(34,12)      |  2
   |(12,111)     |  1
   |(987,321)    |  1
   |(12,123)     |  1
</code></pre>

<p>i am hoping to find answer here, please help me, i have elaborated it as much as possible.</p>