lostsoul29
lostsoul29

Reputation: 756

Pandas pivot certain rows to columns

I have the following pandas DataFrame.

pandas.DataFrame({'C': {64: 128.0, 50: 32.0, 67: 128.0, 52: 32.0, 73: 512.0, 105: 32768.0, 42: 8.0, 47: 8.0, 76: 512.0, 79: 512.0}, 'gamma': {64: 0.0078125, 50: 3.0517578125e-05, 67: 0.5, 52: 0.00048828125, 73: 0.001953125, 105: 0.03125, 42: 0.00048828125, 47: 0.5, 76: 0.125, 79: 8.0}, 'kernel': {64: 'rbf', 50: 'rbf', 67: 'rbf', 52: 'rbf', 73: 'rbf', 105: 'rbf', 42: 'rbf', 47: 'rbf', 76: 'rbf', 79: 'rbf'}, 'std.dev': {64: 0.0063099999999999996, 50: 0.0077600000000000004, 67: 0.0071300000000000001, 52: 0.0066800000000000002, 73: 0.00611, 105: 0.0056100000000000004, 42: 0.0075399999999999998, 47: 0.0058100000000000001, 76: 0.0070000000000000001, 79: 0.0048799999999999998}, 'mean': {64: 0.97031000000000001, 50: 0.94882999999999995, 67: 0.96369000000000005, 52: 0.96518000000000004, 73: 0.96897999999999995, 105: 0.96455000000000002, 42: 0.96267999999999998, 47: 0.96825000000000006, 76: 0.96601999999999999, 79: 0.96560000000000001}})

Output:

     C        gamma   kernel  mean  std.dev
42  8.0     0.000488    rbf 0.96268 0.00754
47  8.0     0.500000    rbf 0.96825 0.00581
50  32.0    0.000031    rbf 0.94883 0.00776
52  32.0    0.000488    rbf 0.96518 0.00668
64  128.0   0.007812    rbf 0.97031 0.00631
67  128.0   0.500000    rbf 0.96369 0.00713
73  512.0   0.001953    rbf 0.96898 0.00611
76  512.0   0.125000    rbf 0.96602 0.00700
79  512.0   8.000000    rbf 0.96560 0.00488
105 32768.0 0.031250    rbf 0.96455 0.00561

I need it into a format where the C column contains only unique values of C, and each unique value in the gamma column becomes a new column. For each such new column of gamma, I would like to show the mean and std.dev for the corresponding values of C and gamma.

Upvotes: 1

Views: 356

Answers (2)

Ted Petrou
Ted Petrou

Reputation: 61957

Just for completeness you can use pivot_table directly.

df.pivot_table(index='C', 
               columns='gamma', 
               values=['mean','std.dev'])\
  .swaplevel(0, 1, axis=1)\
  .sort_index(level=0, axis=1)

Upvotes: 2

piRSquared
piRSquared

Reputation: 294258

use set_index and unstack

df.set_index(['C', 'gamma'])[['mean', 'std.dev']].unstack().swaplevel(0, 1, 1).sort_index(1)

enter image description here

Or equivalently with pivot

pd.concat(
    [df.pivot(index='C', columns='gamma', values=c) for c in ['mean', 'std.dev']],
     keys=['mean', 'std.dev']
).unstack(0)

Upvotes: 1

Related Questions