1

I am trying to stack two columns in one column, but I want to keep a boolean field that denotes where the new values coming from. Using pivot and melt did not yield much success. Any help would be useful. Here is a small example

param | accuracy | accuracy_with_reg
----------------------------
4     |  0.813    | 0.934
5     |  0.456    | 0.654
6     |  0.342    | 0.564  

to the following

param | accuracy | regularized 
----------------------------
4     |  0.813    | False
5     |  0.456    | False
6     |  0.342    | False
4     |  0.934    | True
5     |  0.654    | True
6     |  0.654    | True

Thanks

1 Answer 1

1

You can use melt like

pd.melt(df, id_vars=['param'], value_vars=['accuracy', 'accuracy_with_reg'],
        var_name='regularized', value_name='accuracy_val')

   param        regularized  accuracy_val
0      4           accuracy         0.813
1      5           accuracy         0.456
2      6           accuracy         0.342
3      4  accuracy_with_reg         0.934
4      5  accuracy_with_reg         0.654
5      6  accuracy_with_reg         0.564

if you want True or False in the column regularized, then you have different method but I would go on rename the columns before

pd.melt(df.rename(columns={'accuracy':False, 'accuracy_with_reg':True}), 
        id_vars=['param'], value_vars=[False, True],
        var_name='regularized', value_name='accuracy')
   param regularized  accuracy
0      4       False     0.813
1      5       False     0.456
2      6       False     0.342
3      4        True     0.934
4      5        True     0.654
5      6        True     0.564
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.