我有以下以宽格式设置的数据集,我想将其重塑为长格式。我的标识符是lfdn列和p列。
+------+-------+-------+-----+-----+-----+------+-----+-----+-----+------+-----+-----+-----+
| lfdn | study | i_a | p | p_f | r | i_a | p | p_f | r | i_a | p | p_f | r |
+------+-------+-------+-----+-----+-----+------+-----+-----+-----+------+-----+-----+-----+
| 1 | 1 | 10000 | sgd | 115 | -77 | 405 | sgn | 95 | -77 | 4433 | sgu | 106 | -77 |
| 2 | 1 | 8000 | sgd | 110 | 4 | 6000 | sgn | 105 | 5 | 7000 | sgu | 110 | 4 |
| 3 | 1 | 1000 | sgd | 105 | 5 | 0 | sgn | 103 | 3 | 0 | sgu | 105 | 6 |
| 4 | 1 | 2000 | sgd | 100 | 6 | 5000 | sgn | 110 | 5 | 6000 | sgu | 97 | 4 |
+------+-------+-------+-----+-----+-----+------+-----+-----+-----+------+-----+-----+-----+
我的最终结果应如下所示:
+------+-----+-------+-----+-----+--+
| lfdn | p | i_a | p_f | r | |
+------+-----+-------+-----+-----+--+
| 1 | sgd | 10000 | 115 | -77 | |
| 1 | sgn | 405 | 95 | -77 | |
| 1 | sgu | 4433 | 106 | -77 | |
| 2 | sgd | 8000 | 110 | 4 | |
| 2 | sgn | 6000 | 105 | 5 | |
| 2 | sgu | 7000 | 110 | 4 | |
| 3 | sgd | 1000 | 105 | 5 | |
| 3 | sgn | 0 | 103 | 3 | |
| 3 | sgu | 0 | 105 | 6 | |
| 4 | sgd | 2000 | 100 | 6 | |
| 4 | sgn | 5000 | 110 | 5 | |
| 4 | sgu | 6000 | 97 | 4 | |
+------+-----+-------+-----+-----+--+
编辑:
我的问题是这些列具有相同的名称。因此,建议的重复项中的代码对我不起作用。