Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you don't want the aggregation of <code>pivot_table</code>, you indeed need the <code>pivot</code> function. However, <code>pivot</code> does not work with providing multiple index-columns (actually I don't know why). But, there is a similar function to pivot, <a href="http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.unstack.html" rel="noreferrer"><code>unstack</code></a>, which works the same but based on the (multi) index instead of columns.</p> <p>So to use this, you can first set the columns you want as index/column labels in the result as index:</p> <pre><code>df2 = df.set_index(['firstname','lastname','weight', 'vehicle']) </code></pre> <p>and then unstack on the last level (default), so on 'vehicle' (which become the column labels):</p> <pre><code>In [3]: df2.unstack() Out[3]: speed vehicle bike car plane firstname lastname weight Amy Frond 65.6886 27.101 NaN 344.200 Jon Cho 81.0030 29.022 95.1144 302.952 </code></pre> <p>And if you don't want the multi-index, you can 'flatten' the result with <code>reset_index</code>.<br> The only possible problem that you can have with this is that the columns also have two levels, so you can first remove the first level, and then reset the index to become a really flat dataframe:</p> <pre><code>In [17]: df3 = df2.unstack() In [18]: df3.columns = df3.columns.droplevel(0) In [19]: df3.reset_index() Out[19]: vehicle firstname lastname weight bike car plane 0 Amy Frond 65.6886 27.101 NaN 344.200 1 Jon Cho 81.0030 29.022 95.1144 302.952 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload