0

Given this dataframe:

    ID Sentence.                    Cat_1     Cat_2  Polarity  
0   1  I love this.                 36.1      48     Positive             
1   2  My name is Arthur.           25.2      34     Positive
2   3  I love this game.            28.3      24     Negative
3   4  I need you.                  30.2      45     Negative
4   4  But I don't want you.        68.3      23     Neutral

I'd like to have this dataframe pivoting like this:

    ID  Sentence           Category   Cat_Score TopicSentiment
 0   1  I love this.       Cat_1      36.1      Positive   
 1   1  I love this.       Cat_2      48        Positive
 2   2  My name is Arthur. Cat_1      25.2      Positive
 3   2  My name is Arthur. Cat_2      34        Positive
 ... 

So Cat_1 and Cat_2 will become a value of the column Category and the score will become a value of the column Cat_Score.

  • df.melt(id_vars=["ID", "Sentence","Polarity"], var_name="Cat", value_name="Cat_Score").sort_values('Sentence') – Rictrunks Mar 15 '22 at 10:51

0 Answers0