转换数据帧,其中行数据用作列

时间:2018-03-08 16:18:11

标签: python pandas pandas-groupby

我已经坚持了一段时间,尝试了各种排列。我有一个大型数据集,需要从图像[左]转换为图像[右]

我认为它需要拆散,堆叠和分组......但似乎无法弄明白。

enter image description here

print(df.head(200))
                                  epic_fullname        key            status
0     [XTA-10261] - EQD Delta One and EMD EPIC   XTBOW-970            Closed
17    [XTA-10261] - EQD Delta One and EMD EPIC   XTA-12114         Scheduled
49    [XTA-10261] - EQD Delta One and EMD EPIC   XTA-12044          Resolved
54    [XTA-10261] - EQD Delta One and EMD EPIC   XTA-12036     Awaiting Test
99    [XTA-10261] - EQD Delta One and EMD EPIC   XTA-11849          Resolved
100   [XTA-10261] - EQD Delta One and EMD EPIC   XTA-11840          Resolved
102   [XTA-10261] - EQD Delta One and EMD EPIC   XTA-11832          Resolved
113   [XTA-10261] - EQD Delta One and EMD EPIC   XTA-11794          Resolved
116   [XTA-10261] - EQD Delta One and EMD EPIC   XTA-11783          Resolved
294   [XTA-10261] - EQD Delta One and EMD EPIC   XTA-10739          Resolved
302   [XTA-10261] - EQD Delta One and EMD EPIC   XTA-10258          Resolved
303   [XTA-10261] - EQD Delta One and EMD EPIC   XTA-10257          Resolved
304   [XTA-10261] - EQD Delta One and EMD EPIC   XTA-10256          Resolved
374  [XTA-11427] - IDX MIGTN - Valuation & Risk  XTA-11770          Resolved
376  [XTA-11427] - IDX MIGTN - Valuation & Risk  XTA-11703          Resolved
394  [XTA-11427] - IDX MIGTN - Valuation & Risk  XTA-11529          Resolved
397  [XTA-11427] - IDX MIGTN - Valuation & Risk  XTA-11504          Resolved
426  [XTA-11427] - IDX MIGTN - Valuation & Risk  XTA-11265          Resolved
427  [XTA-11427] - IDX MIGTN - Valuation & Risk  XTA-11264    In Development
428  [XTA-11427] - IDX MIGTN - Valuation & Risk  XTA-11263          Resolved
429  [XTA-11427] - IDX MIGTN - Valuation & Risk  XTA-11262          Resolved
438  [XTA-11427] - IDX MIGTN - Valuation & Risk  XTA-11089          Resolved
482  [XTA-11427] - IDX MIGTN - Valuation & Risk  XTA-10858  Awaiting Release
516  [XTA-11427] - IDX MIGTN - Valuation & Risk  XTA-10691          Resolved
519  [XTA-11427] - IDX MIGTN - Valuation & Risk  XTA-10686          Resolved

1 个答案:

答案 0 :(得分:1)

您需要unstack操作:

v = df.set_index(['epic_fullname', 'status'])

df = v.set_index(
         v.groupby(level=[0, 1]).cumcount(), append=True
     ).key\
      .unstack(-2)\
      .fillna('')

enter image description here 您还需要一个额外的索引列,以便您可以取消堆栈。 groupby + cumcount操作就是为了这个目的。