Convert Pandas Dataframe from Row based to Columnar
<p>My Dataframe (df) looks like this:</p>

<pre><code>Date       FieldA  ValueA ValueB
09-02-2016 TypeA   3       5
09-02-2016 TypeB   6       7
</code></pre>

<p>I want the dataframe to look like below:</p>

<pre><code>Date       TypeA_ValueA   TypeA_ValueB  TypeB_ValueA TypeB_ValueB
09-02-2016  3             5             6            7
</code></pre>

<p>I tired the df.pivot in pandas where I can provide single Value column. It doesnt take more than one. When I provide more than one i get below exception. <a href="http://pandas.pydata.org/pandas-docs/stable/reshaping.html" rel="nofollow">pandas_pivot</a></p>

<pre><code>Exception: Data must be 1-dimensional
</code></pre>

<pre><code>df1 = df.set_index(['Date', 'FieldA']).unstack()
df1.columns = df1.columns.map('_'.join)

df1.reset_index()
</code></pre>

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

<hr>

<h3>Setup Reference</h3>

<pre><code>from StringIO import StringIO
import pandas as pd

text = """Date       FieldA  ValueA ValueB
09-02-2016 TypeA   3       5
09-02-2016 TypeB   6       7"""

df = pd.read_csv(StringIO(text), delim_whitespace=True)

df
</code></pre>

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

python sql pandas pyspark spark-dataframe