我有一个在status
,date
上索引的数据帧。大多数变量对status
和date
都是特定的,但是有superDuper
只对date
是特定的,对每个状态都是相同的:
t070199 t070299 t070201 t070105 superDuper
date status
2003-01-01 foo 0.137684 0.032750 1.729324 4.484633 481.329224
fubar 1.348261 0.000000 0.159443 0.636001 481.329224
2005-01-01 foo 0.038752 0.006538 1.341507 3.518619 474.549400
fubar 0.000000 0.000000 0.000000 8.457774 474.549400
2007-01-01 foo 0.000000 0.097280 3.639593 1.901233 459.767016
如果我做了df.unstack(level='status')
,我会得到一些
t070199 t070299 superDuper t070105 \
status foo fubar foo fubar foo fubar emp
date
2003-01-01 0.137684 1.348261 0.032750 0 1.729324 1.729324 4.484633
2005-01-01 0.038752 0.000000 0.006538 0 1.341507 1.341507 3.518619
2007-01-01 0.000000 0.000000 0.097280 0 3.639593 3.639593 1.901233
2009-01-01 0.010630 0.000000 0.000000 0 1.840457 1.840457 2.728080
2011-01-01 0.013474 0.000000 0.000000 0 2.020553 1.739148 1.320693
也就是说,有一个包含列txxxx
、superDuper
的第一级,对于每个列,我们有两个子列。你知道吗
我想重新调整数据的形状,以便有以下第一级列:
[`t070199`, `t070299`, `t070201`, `t070105`]
对于每个顶层列,我希望有3个子列:
['foo', 'bar', 'superDuper']
然后作为索引,只有date
。由于superDuper
不是特定于txxxx
列的,这可能意味着重复该列。pivot
并不能让我达到这个目的,而且因为它不是一个子层,unstack()
可能也不起作用。我可以尝试什么方法?你知道吗
让我们从数据帧开始:
弹出
superDuper
并保存以备以后使用。现在可以使用unstack。你知道吗获取唯一的
superDuper
值:现在,我假设您希望每个txxxx都有一个子列。从你的问题上看不太清楚这是真的还是你只是听天由命。你知道吗
你可以使用听写理解来获得新的列。你知道吗
现在只需加入并排序:
相关问题 更多 >
编程相关推荐