print df
output_time position
0 2016-01-01 08:00:01 start
1 2016-01-01 08:07:53 end
2 2016-01-01 08:07:54 start
3 2016-01-01 08:09:23 end
4 2016-01-01 08:09:24 start
5 2016-01-01 08:32:51 end
我需要这样的输出(df仍然是数据帧类型而不是序列类型):
print df
start end
2016-01-01 08:00:01 2016-01-01 08:07:53
2016-01-01 08:07:54 2016-01-01 08:09:23
2016-01-01 08:09:24 2016-01-01 08:32:51
df = df.pivot_table(columns="output_time", values="position")
给出以下错误:
引发DataError('No numeric types to aggregate')pandas.core.base.DataError: No numeric types to aggregate
用pivot
代替pivot_table
:
# Perform the pivot.
df = df.pivot(index=df.index//2, columns='position')
# Format the columns.
df.columns = df.columns.droplevel(0).rename(None)
结果输出:
end start
0 2016-01-01 08:07:53 2016-01-01 08:00:01
1 2016-01-01 08:09:23 2016-01-01 08:07:54
2 2016-01-01 08:32:51 2016-01-01 08:09:24
使用cumcount
创建新的index
值和unstack
的另一个解决方案:
df['g'] = df.groupby('position').cumcount()
df1 = df.set_index(['g','position']).unstack()
df1.columns = df1.columns.droplevel(0)
df1.columns.name = None
df1.index.name = None
print (df1)
end start
0 2016-01-01 08:07:53 2016-01-01 08:00:01
1 2016-01-01 08:09:23 2016-01-01 08:07:54
2 2016-01-01 08:32:51 2016-01-01 08:09:24