1

I have this CSV data

C,kernel,error
0.001,linear,0.03
0.001,poly,0.04
0.01,linear,0.01
0.01,poly,0.03
0.1,linear,0.00
0.1,poly,0.02
1.0,linear,0.00
1.0,poly,0.01
10.0,linear,0.00
10.0,poly,0.01

Using pandas, how can I create a dataframe that contains a column of unique C values, the kernel associated to that C value as well as the error?

expected output

C, linear, poly
0.001,0.03,0.04  <-- error values
0.01,0.01,0.03
0.1,0.00,0.02
1.0,0.00,0.01
10,0.00,0.01
Liondancer
  • 15,721
  • 51
  • 149
  • 255

1 Answers1

3

You can use pd.DataFrame.pivot:

res = df.pivot(index='C', columns='kernel', values='error')

print(res)

kernel  linear  poly
C                   
0.001     0.03  0.04
0.010     0.01  0.03
0.100     0.00  0.02
1.000     0.00  0.01
10.000    0.00  0.01
jpp
  • 159,742
  • 34
  • 281
  • 339
  • I am trying to plot this data as well but I see 6 values in the X axis. but there are only 5 rows, C against Error. Why is this? Also how can I get the C values as labels in the X axis? – Liondancer Sep 23 '18 at 23:11
  • @Liondancer, Not sure. You should [ask a separate question](https://stackoverflow.com/questions/ask) since this is a different question. – jpp Sep 23 '18 at 23:12