Pandas - Data Frame - Reshaping Values in Data Frame
<p>I am new to Pandas and have a data frame with a team's score in 2 separate columns. This is what I have.</p>

<pre><code>Game_ID Teams   Score

1    Team A  95
1    Team B  85
2    Team C  90
2    Team D  72
</code></pre>

<p>This is where I would like to get to and then ideally to.</p>

<pre><code>1   Team A  95 Team B  94
2   Team C  90 Team B  72 
</code></pre>

<p>You can try something as follows: Create a <code>row_id</code> within each group by the <code>Game_ID</code> and then unstack by the <code>row_id</code> which will transform your data to wide format:</p>

<pre><code>import pandas as pd
df['row_id'] = df.groupby('Game_ID').Game_ID.transform(lambda g: pd.Series(range(g.size)))
df.set_index(['row_id', 'Game_ID']).unstack(level=0).sortlevel(level = 1, axis = 1)
</code></pre>

<p><a href="https://i.stack.imgur.com/Lsl0a.png" rel="nofollow"><img src="https://i.stack.imgur.com/Lsl0a.png" alt="enter image description here"></a></p>

<p><em>Update</em>:</p>

<p>If the <code>row_id</code> is preferred to be dropped, you can drop the level from the columns:</p>

<pre><code>df1 = df.set_index(['row_id', 'Game_ID']).unstack(level=0).sortlevel(level = 1, axis = 1)   
df1.columns = df1.columns.droplevel(level = 1)
df1
</code></pre>

<p><a href="https://i.stack.imgur.com/aF5Gc.png" rel="nofollow"><img src="https://i.stack.imgur.com/aF5Gc.png" alt="enter image description here"></a></p>

python pandas dataframe