Select certain row values and make them columns in pandas
<p>I have a dataset that looks like the below:</p>

<pre><code>+-------------------------+-------------+------+--------+-------------+--------+--+
|                         | impressions | name | shares | video_views |  diff  |  |
+-------------------------+-------------+------+--------+-------------+--------+--+
| _ts                     |             |      |        |             |        |  |
| 2016-09-12 23:15:04.120 |           1 | Vidz |      7 |       10318 | 15mins |  |
| 2016-09-12 23:16:45.869 |           2 | Vidz |      7 |       10318 | 16mins |  |
| 2016-09-12 23:30:03.129 |           3 | Vidz |     18 |       29291 | 30mins |  |
| 2016-09-12 23:32:08.317 |           4 | Vidz |     18 |       29291 | 32mins |  |
+-------------------------+-------------+------+--------+-------------+--------+--+
</code></pre>

<p>I am trying to build a dataframe to feed to a regression model, and I'd like to parse out specific rows as features.  To do this I would like the dataframe to resemble this</p>

<pre><code>+-------------------------+------+--------------+-------------------+-------------------+--------------+-------------------+-------------------+
|                         | name | 15min_shares | 15min_impressions | 15min_video_views | 30min_shares | 30min_impressions | 30min_video_views |
+-------------------------+------+--------------+-------------------+-------------------+--------------+-------------------+-------------------+
| _ts                     |      |              |                   |                   |              |                   |                   |
| 2016-09-12 23:15:04.120 | Vidz |            7 |                 1 |             10318 |           18 |                 3 |             29291 |
+-------------------------+------+--------------+-------------------+-------------------+--------------+-------------------+-------------------+
</code></pre>

<p>What would be the best way to do this?  I think this would be easier if I were only trying to select 1 row (15mins), just parse out the unneeded rows and pivot.</p>

<p>However, I need 15min and 30min features and am unsure on how to proceed of the need for these columns</p>

<p>stacking to pivot and collapsing your columns</p>

<pre><code>df1 = df.set_index('diff', append=True).stack().unstack(0).T
df1.columns = df1.columns.map('_'.join)
</code></pre>

<hr>

<p>To see just the first row</p>

<pre><code>df1.iloc[[0]].dropna(1)
</code></pre>

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

python pandas