1
$\begingroup$

I currently have a pandas dataframe with the following format

    Model      Metric                     Value
--------------------------------------------------------------
0     Ours     Accuracy  [0.79, 0.79, 0.82]
1   Theirs     Accuracy  [0.68, 0.56, 0.64]
2     Ours  Sensitivity  [0.64, 0.55, 0.55]
3   Theirs  Sensitivity  [0.82, 0.82, 0.78]
4     Ours  Specificity  [0.68, 0.48, 0.6] 
5   Theirs  Specificity  [0.68, 0.48, 0.6]

In the evaluation script I am writing I want to be able to take into account situations where the training of a model is repeated multiple times, the results are stored in an numpy array (Value column). For visualization with seaborn, i believe i need a long form where it looks something like this:

    Model      Metric                     Value
--------------------------------------------------------------
0     Ours     Accuracy                   0.79
1     Ours     Accuracy                   0.79
2     Ours     Accuracy                   0.82
3     Theirs   Accuracy                   0.68
4     Theirs   Accuracy                   0.56
5     Theirs   Accuracy                   0.64
6     Ours     Sensitivity                0.64
...   ....     ...                        ...

I cannot figure out how to do this.

$\endgroup$
0
$\begingroup$
# create the df
df = pd.DataFrame({'model':['our','theirs'],'metric':['acc','sen'],'value':[np.array([1,2,3]),np.array([4,5,6])]})
# unzip the array into three columns
df[['ex1','ex2','ex3']]=pd.DataFrame(df['value'].to_list(),index=df.index)
# melt df to long df
df.melt(id_vars=['model','metric'],value_vars=['ex1','ex2','ex3'])

You may need to sort the dataframe as you like.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Thank you, it works! One small correction, I needed to change to_list() into tolist() for it to work. $\endgroup$ – Joost vd Putten Dec 18 '19 at 10:30
  • $\begingroup$ Guess it depends on pandas version. xD $\endgroup$ – chzhrr Dec 18 '19 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.