我有以下格式的数据,其中包含不同月份的多个度量列,如下所示
Cust_No Measure1_month1 Measure1_month2 .... Measure1_month72 Measure2_month_1 Measure2_month_2....so on
1 10 20 .... 500 40 50
2 20 40 .... 800 70 150 ....
我想实现以下两种格式。 格式1)
+-------------+----------+---------+-------+
| CustNum | Measure | Value | Month |
+-------------+----------+---------+-------+
| 1 | Measure1 | 10 | 1 |
| 1 | Measure1 | 20 | 2 |
| 1 | Measure1 | 30 | 3 |
| 1 | Measure1 | 70 | 4 |
| 1 | Measure1 | 40 | 5 |
| . | . | . | . |
| . | . | . | . |
| 1 | Measure1 | 700 | 72 |
| 1 | Measure2 | 30 | 1 |
| 1 | Measure2 | 40 | 2 |
| 1 | Measure2 | 80 | 3 |
| 1 | Measure2 | 90 | 4 |
| 1 | Measure2 | 100 | 5 |
| . | . | . | . |
| . | . | . | . |
| . | . | . | . |
| 1 | Measure2 | 50 | 72 |
+-------------+----------+---------+-------+
每一个客户号都是如此
格式2:
+---------+---------+----------+----------+
| CustNum | Month | Measure1 | Measure2 |
+---------+---------+----------+----------+
| 1 | 1 | 10 | 30 |
| 1 | 2 | 20 | 40 |
| 1 | 3 | 30 | 80 |
| 1 | 4 | 70 | 90 |
| 1 | 5 | 40 | 100 |
| . | . | . | . |
| . | . | . | . |
| 1 | 72 | 700 | 50 |
+---------+---------+----------+----------+
每一个客户号都是如此
你能帮我做这个吗
谢谢
给定输入数据帧,df为:
输出:
格式1:
输出:
格式2:
输出:
设置
有很多争论,但一般来说:将列拆分为多索引,然后堆叠。您需要的第二种格式是第一种格式的轴心
输出
相关问题 更多 >
编程相关推荐