Finding the consecutive pairs of the values in a row and then operating on them in python
<p>I have a data frame in python like: <br></p>

<pre><code>item  Value

abc   3
xyz   5
pqr   7
abc   3
pqr   7
abc   5
xyz   5
</code></pre>

<p>Now I want to add the first occurrence of any value and the second occurrence of that item's value in pairs,
so the output should be like:-<br></p>

<pre><code>item  Value

abc   6
abc   8
xyz   10
pqr   14
</code></pre>

<p>This is more of a question for logic than pandas but since you asked in pandas, there is a quick way to do this:</p>

<pre><code>df['value_to_add'] = df.sort_values('item').groupby('item').shift(-1)
df.dropna(inplace=True) 
df['value'] = df.value + df.value_to_add
df.drop('value_to_add', inplace=True, axis=1)
</code></pre>

python pandas