Python堆叠到未堆叠的格式

时间:2017-03-16 15:50:08

标签: python database pandas

或者也称为长到宽格式。

我有以下内容:

ID1    ID2   POS1    POS2    TYPE     TYPEVAL
---    ---   ----    ----    ----     -------
A      001   1       5       COLOR    RED
A      001   1       5       WEIGHT   50KG
A      001   1       5       HEIGHT   160CM
A      002   6       19      FUTURE   YES
A      002   6       19      PRESENT  NO
B      001   26      34      COLOUR   BLUE
B      001   26      34      WEIGHT   85KG
B      001   26      34      HEIGHT   120CM
C      001   10      13      MOBILE   NOKIA      
C      001   10      13      TABLET   ASUS

我希望根据每个唯一值将TYPE列转换为新列,即

ID1    ID2    POS1    POS2    COLOR    WEIGHT    HEIGHT    FUTURE    PRESENT    MOBILE    TABLET
A      001    1       5       RED      50KG      160CM     NA        NA         NA        NA
A      002    6       19      NA       NA        NA        YES       NO         NA        NA
B      001    26      34      BLUE     85KG      120CM     NA        NA         NA        NA
C      001    10      13      NA       NA        NA        NA        NA         NOKIA     ASUS    

我尝试过以下方式:

PD.pivot_table(df,index=["ID1","ID2"],columns=["BEGIN","END","TYPE"],values=["TYPEVAL"])

然而我得到了:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pandas/tools/pivot.py", line 127, in pivot_table
    agged = grouped.agg(aggfunc)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.py", line 3690, in aggregate
    return super(DataFrameGroupBy, self).aggregate(arg, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.py", line 3179, in aggregate
    result, how = self._aggregate(arg, _level=_level, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/base.py", line 432, in _aggregate
    return getattr(self, arg)(*args, **kwargs), None
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.py", line 1009, in mean
    return self._cython_agg_general('mean')
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.py", line 3113, in _cython_agg_general
    how, numeric_only=numeric_only)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.py", line 3159, in _cython_agg_blocks
    raise DataError('No numeric types to aggregate')

我被提示通过某个数字函数(即均值或求和)聚合列。但是我想不做这样的事情,我只想转移TYPE列而不进行任何聚合。

任何建议都会非常感谢!

2 个答案:

答案 0 :(得分:3)

您可以使用'TYPEVAL'列以外的所有列设置索引,然后unstack

df.set_index(
    df.columns.difference(['TYPEVAL']).tolist()
).TYPEVAL.unstack('TYPE').reset_index().rename_axis(None, axis=1)

enter image description here

答案 1 :(得分:2)

我认为您需要pivot_table汇总first或多个值joinsum,因为deafult汇总函数为mean且仅适用于数值:

df1 = pd.pivot_table(df,
                     index=["ID1","ID2","POS1","POS2",],
                     columns="TYPE",
                     values="TYPEVAL",
                     aggfunc='first')
       .reset_index().rename_axis(None, axis=1)

print (df1)
  ID1  ID2  POS1  POS2 COLOR COLOUR FUTURE HEIGHT MOBILE PRESENT TABLET WEIGHT
0   A    1     1     5   RED   None   None  160CM   None    None   None   50KG
1   A    2     6    19  None   None    YES   None   None      NO   None   None
2   B    1    26    34  None   BLUE   None  120CM   None    None   None   85KG
3   C    1    10    13  None   None   None   None  NOKIA    None   ASUS   None

df1 = pd.pivot_table(df,
                     index=["ID1","ID2","POS1","POS2",],
                     columns="TYPE",
                     values="TYPEVAL",
                     aggfunc=','.join)
       .reset_index().rename_axis(None, axis=1)
print (df1)
  ID1  ID2  POS1  POS2 COLOR COLOUR FUTURE HEIGHT MOBILE PRESENT TABLET WEIGHT
0   A    1     1     5   RED   None   None  160CM   None    None   None   50KG
1   A    2     6    19  None   None    YES   None   None      NO   None   None
2   B    1    26    34  None   BLUE   None  120CM   None    None   None   85KG
3   C    1    10    13  None   None   None   None  NOKIA    None   ASUS   None