我的数据框看起来像这样 -
project name comment1 comment2 comment3 comment4
Row1 NY Not good somewhat ok ridiculous satisfactory
Row2 LA job well done standard under performance standard
如何将其转换为如下所示?
project name comment
Row1 NY Not good
Row2 NY Somewhat Ok
Row3 NY ridiculous
Row4 NY satisfactory
Row5 LA job well done
Row6 LA standard
Row7 LA under performance
Row8 LA standard
这是一个转置操作,但只有comment1到comment4被转置。在SAS中,可以创建阵列。但我不知道如何在python中解决问题。有没有人这样做过?
答案 0 :(得分:2)
您需要melt
:
df.melt(id_vars='project name', value_name='comment').drop('variable', axis=1)\
.sort_values(by='project name')
输出:
project name comment
1 LA job well done
3 LA standard
5 LA under performance
7 LA standard
0 NY Not good
2 NY somewhat ok
4 NY ridiculous
6 NY satisfactory