0

I've this issue, I don't know maybe I'm getting tired, I've this dataframe

tostack=pd.DataFrame([['tol', 0.001],
       ['solver', 'svd'],
       ['alpha', 0.001],
       ['tol', 0.01],
       ['solver', 'cholesky'],
       ['alpha', 0.001],
       ['tol', 1.0],
       ['solver', 'svd'],
       ['alpha', 1.0]], columns=["param_name",'param_set'])

I need to "pivot" this into this shape :

tol      solver      alpha
0.001      'svd'     0.01
0.01    'cholesky'   0.001
1          'svd'     1.0

I've tried to pivot but I get "nan" between rows with following code

pd.DataFrame(dic_loc, columns=["param_name",'param_set'])\
    .pivot( columns='param_name', values='param_set')

So, what I need it's to iterate over the items and append the values by row, for each 3 values.

Any ideas are welcome,

Carlos Carvalho
  • 131
  • 1
  • 8
  • 3
    Refer to Q10 in the dup link. – Quang Hoang Mar 13 '20 at 16:46
  • 1
    you need pass aggfunc = first or ''.join, `tostack.pivot_table(index=tostack.groupby(tostack['param_name']).cumcount(),columns = 'param_name',values = 'param_set',aggfunc='first')` – ansev Mar 13 '20 at 16:48

1 Answers1

1

Try this:

>>> new_df = tostack.groupby('param_name').agg(list)

>>> pd.DataFrame(new_df['param_set'].values.tolist(), columns=new_df.index)

param_name  alpha    solver  tol
0           0.001     0.001    1
1             svd  cholesky  svd
2           0.001      0.01    1
Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52