Pandas将一列值(onehotencoding)映射到具有多个可能值的单个列中

2024-04-19 10:36:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个这种形状的数据框:

anger fear joy love sadness surprise thankfulness disgust guilt
0   1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
1   1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
2   1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
3   1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
4   1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

您可以注意到情绪标签['anger', 'fear', 'joy', 'love', 'sadness', 'surprise', 'thankfulness', 'disgust', 'guilt']是如何以基于列的方式格式化的,我想做的是,我们添加另一列(情绪),以便它根据预定义的字典存储情绪标签,而不是多个列:

{0: 'anger',
 1: 'fear',
 2: 'joy',
 3: 'love',
 4: 'sadness',
 5: 'surprise',
 6: 'thankfulness',
 7: 'disgust',
 8: 'guilt'}

示例输出(不同的数据帧):

    Emotion Text
0   joy During the period of falling in love, each tim...
1   fear    When I was involved in a traffic accident.
2   anger   When I was driving home after several days of...
3   sadness When I lost the person who meant the most to me.
4   disgust The time I knocked a deer down - the sight of ...

我正在寻找一种更像熊猫的方法来做到这一点


Tags: ofthe数据标签surprisewhen情绪love