title: Python column and row interaction in dataframe
body: <p>Let's imagine I have a dataframe :</p>
  
  <pre><code>question  user   level 
      1      a       1     
      1      b       2     
      1      a       3     
      2      a       1     
      2      b       2     
      2      a       3     
      2      b       4     
      3      c       1     
      3      b       2     
      3      c       3     
      3      a       4     
      3      b       5
  </code></pre>
  
  <p>Column level specifies who started the topic and who replied to it. If user's level is 1, it means that he asked the question. If user's level is 2, it means that he replied to the user who asked the question. If user's level is 3, it means that he replied to the user whose level is 2 and so on.</p>
  
  <p>I would like to extract a new dataframe that should present a communication between users through question. It should contain three columns: "user source", "user destination" and "reply count". Reply count is a number of times in which User Destination "directly" replied to User Source.</p>
  
  <pre><code>    us_source us_dest reply_count
          a        b       2
          a        c       0
          b        a       0
          b        c       0
          c        a       0
          c        b       1
  </code></pre>
  
  <p>I tried to find first two columns using this code..</p>
  
  <pre><code>idx_cols = ['question']
  std_cols = ['user_x', 'user_y']
  df1 = df.merge(df, on=idx_cols)
  df2 = df1.loc[f1.user_x != f1.user_y, idx_cols + std_cols]
  
  df2.loc[:, std_cols] = np.sort(df2.loc[:, std_cols])
  </code></pre>
  
  <p>Does anyone have some suggestions for the third column?
  Consider "direct" a reply from B to A if and only if B replied at level k to a message of A at level k-1 in the same topic.  If a topic is started by student A (by sending a message at level 1), B replied to A (sending a message at level 2), so B directly replied to A. Only students' replies from level 2 to level 1.</p>