Python DataFrame Transform Rows to Column
<p>I have a csv that stores information about a particular object and Date. </p>

<pre><code>Device  Date        Category    Amount
Pen     01/01/2014  A           12
Pen     01/01/2014  B           42
Pen     01/01/2014  C           10
Pen     01/01/2014  D           5
Pen     02/01/2014  A           7
Pen     02/01/2014  B           52
Pen     02/01/2014  C           1
Pen     02/01/2014  D           7
Pencil  01/01/2014  A           22
Pencil  01/01/2014  B           42
Pencil  01/01/2014  C           70
Pencil  01/01/2014  D           8
</code></pre>

<p>I want to read it into a DataFrame and make Category a column and Amounts for a specific device and date a row. This will make the dataset much smaller. </p>

<pre><code>Device  Date        A   B   C   D
Pen    01/01/2014   12  42  10  5
Pen    02/01/2014   7   52  1   7
Pencil  01/01/2014  22  42  70  8
</code></pre>

<p>You can use <code>pivot_table</code> where columns that you want to keep are set as <code>index</code>, columns that go to the header are set as <code>columns</code> and columns that fill the cells in the output data frame are set as <code>values</code>:</p>

<pre><code>df.pivot_table(index=['Device', 'Date'], columns='Category', values='Amount').reset_index()

# Category  Device        Date     A     B   C  D
#        0     Pen  01/01/2014    12    42  10  5
#        1     Pen  02/01/2014     7    52   1  7
#        2  Pencil  01/01/2014    22    42  70  8
</code></pre>

python pandas dataframe