【发布时间】:2016-10-05 20:18:01
【问题描述】:
我的数据目前是长格式。下面是一个示例:
Stock Date Time Price Year
AAA 2001-01-05 15:20:09 2.380 2001
AAA 2002-02-23 10:13:24 2.440 2002
AAA 2002-02-27 17:17:55 2.460 2002
BBB 2006-05-13 16:03:49 2.780 2006
BBB 2006-10-04 10:33:10 2.800 2006
我想通过“Stock”和“Year”将其重塑为宽格式,如下所示:
Stock Year Date1 Time1 Price1 Date2 Time2 Price2
AAA 2001 2001-01-05 15:20:09 2.380
AAA 2002 2002-02-23 10:13:24 2.440 2002-02-27 17:17:55 2.460
BBB 2006 2006-05-13 16:03:49 2.780 2006-10-04 10:33:10 2.800
我尝试了这里发布的解决方案 Pandas long to wide reshape 并得到了这个:
df['idx'] = df.groupby(['Stock', 'Year']).cumcount()
df['date_idx'] = 'date_' + df.idx.astype(str)
df['time_idx'] = 'time_' + df.idx.astype(str)
df['price_idx'] = 'price_' + df.idx.astype(str)
date = df.pivot(index=['Stock', 'Year'], columns='date_idx', values='Date')
time = df.pivot(index=['Stock', 'Year'], columns='time_idx', values='Time')
price = df.pivot(index=['Stock', 'Year'], columns='price_idx', values='Price')
reshape = pd.concat([date, time, price], axis=1)
但最后一行给了我这个错误:
ValueError: 传递的项目数错误 15624,位置暗示 2
我的代码哪里出错了?还是有另一种更清洁的方式来进行这种重塑?
【问题讨论】:
-
我列举了几个例子here
标签: python pandas dataframe pivot-table reshape