熊猫-使用列名重构数据框

时间:2018-09-14 16:48:32

标签: python pandas dataframe

我阅读了下面的DataFrame,其中有许多与S&PCOMP相似的列,在列名的末尾带有(PO)和(PI)尾部。

                      Date         S&PCOMP(PO)         S&PCOMP(PI)  NASA100(PO)    NASA100(PI)
0      1978-09-13 00:00:00                 nan              106.34  someValue      someValue 
1      1978-09-14 00:00:00                 nan  105.10000000000001  someValue      someValue
2      1978-09-15 00:00:00                 nan              104.12   
3      1978-09-18 00:00:00                 nan  103.21000000000001   
4      1978-09-19 00:00:00                 nan              102.53   
5      1978-09-20 00:00:00                 nan              101.73   
6      1978-09-21 00:00:00                 nan               101.9   
7      1978-09-22 00:00:00                 nan              101.84   
8      1978-09-25 00:00:00                 nan              101.86   
9      1978-09-26 00:00:00                 nan              102.62   
10     1978-09-27 00:00:00                 nan              101.66

我想使用正则表达式和多索引针对每个单个列名将其重组为以下DataFrame。本质上,我将PO和PI值用作2列,并使用列名称库作为索引垂直扩展DF。如您所见,日期滚动……意味着对于每个唯一的列名称库,我都有相同的天数。

                      Date                      Open               Close
S&PCOMP      1978-09-13 00:00:00                 nan              106.34   
S&PCOMP      1978-09-14 00:00:00                 nan  105.10000000000001   
S&PCOMP      1978-09-15 00:00:00                 nan              104.12   
S&PCOMP      1978-09-18 00:00:00                 nan  103.21000000000001   
S&PCOMP      1978-09-19 00:00:00                 nan              102.53   
S&PCOMP      1978-09-20 00:00:00                 nan              101.73   
S&PCOMP      1978-09-21 00:00:00                 nan               101.9   
S&PCOMP      1978-09-22 00:00:00                 nan              101.84   
S&PCOMP      1978-09-25 00:00:00                 nan              101.86   
S&PCOMP      1978-09-26 00:00:00                 nan              102.62   
S&PCOMP      1978-09-27 00:00:00                 nan              101.66
NASA100      1978-09-13 00:00:00                 someValue        someValue   
NASA100      1978-09-14 00:00:00                 someValue        someValue  

用大熊猫实现这一目标的最简单方法是什么?我可以使用正则表达式吗?

0 个答案:

没有答案