我有以下数据框:
City dt Stat1
0 YYZ 2015-12-31 -562.14
1 YYZ 2016-01-31 -701.18
2 YYZ 2016-02-29 -265.44
3 YYZ 2016-03-31 -42.62
4 YYZ 2016-04-30 -468.95
5 YYZ 2016-05-31 -549.94
6 YYZ 2016-06-30 80.84
7 YYZ 2016-07-31 -633.36
8 YYZ 2016-08-31 -1700.73
9 YYZ 2016-09-30 -229.40
10 YYZ 2016-10-31 996.27
11 YYZ 2016-11-30 117.01
{'City': {0: 'YYZ',
1: 'YYZ',
2: 'YYZ',
3: 'YYZ',
4: 'YYZ',
5: 'YYZ',
6: 'YYZ',
7: 'YYZ',
8: 'YYZ',
9: 'YYZ',
10: 'YYZ',
11: 'YYZ'},
'dt': {0: Timestamp('2015-12-31 00:00:00'),
1: Timestamp('2016-01-31 00:00:00'),
2: Timestamp('2016-02-29 00:00:00'),
3: Timestamp('2016-03-31 00:00:00'),
4: Timestamp('2016-04-30 00:00:00'),
5: Timestamp('2016-05-31 00:00:00'),
6: Timestamp('2016-06-30 00:00:00'),
7: Timestamp('2016-07-31 00:00:00'),
8: Timestamp('2016-08-31 00:00:00'),
9: Timestamp('2016-09-30 00:00:00'),
10: Timestamp('2016-10-31 00:00:00'),
11: Timestamp('2016-11-30 00:00:00')},
'Stat1': {0: -562.13999999999999,
1: -701.18000000000029,
2: -265.43999999999994,
3: -42.620000000000012,
4: -468.9500000000001,
5: -549.94000000000005,
6: 80.840000000000032,
7: -633.36000000000013,
8: -1700.7300000000002,
9: -229.40000000000006,
10: 996.26999999999998,
11: 117.01000000000001}}
我想将dt
列中的值移动到各列。
所以结果看起来应该是这样的:
City, 2015-12-31, 2016-01-31, ...
YYZ 562.14, -701.18, ...
答案 0 :(得分:4)
使用pivot
重塑您的DataFrame。
df.pivot(index='City', columns='dt', values='Stat1')