将元组列表转换为具有多级列的数据框

时间:2018-10-18 03:25:48

标签: python pandas

我有一个要转换为数据框的namedtuple列表。元组是这样的:

s = pd.Series({'A': 1, 'B': 2}, 
              pd.Index([u'A', u'B'], 
                       name=u'submission_label'))

SingleExperimentStatistics = namedtuple('SingleExperimentStatistics', 
['metric_name', 'z_score', 'average'])

res = SingleExperimentStatistics(
  metric_name=None, 
  z_score=1.1826795129064109, 
  average=s,
)

致电pd.Dataframe([res, res])给我们

   metric_name  z_score  average
0  None         1.18268  submission_label A 1 B 2 dtype: int64
1  None         1.18268  submission_label A 1 B 2 dtype: int64 

但是我想要的是带有MultiIndex列的数据透视表,其中AB是列名。基本上是这样的:

   metric_name  z_score  average
                         A   B   
0  None         1.18268  1   2
1  None         1.18268  1   2 

正确的做法是什么?

1 个答案:

答案 0 :(得分:2)

我希望通过将这些系列分为两列来使用简单的索引:

import pandas as pd
from collections import namedtuple
s = pd.Series({'A': 1, 'B': 2}, 
              pd.Index([u'A', u'B'], 
                       name=u'submission_label'))
SingleExperimentStatistics = namedtuple('SingleExperimentStatistics', 
    ['metric_name', 'z_score', 'average'])
res = SingleExperimentStatistics(
  metric_name=None, 
  z_score=1.1826795129064109, 
  average=s,
)

df = pd.DataFrame([res, res])
df1 = df.loc[:, ['metric_name', 'z_score']]
df1['A'] = df['average'].apply(lambda x: x['A'])
df1['B'] = df['average'].apply(lambda x: x['B'])
print(df1)

  metric_name  z_score  A  B
0        None  1.18268  1  2
1        None  1.18268  1  2

如果您确实想要多索引,则可以在此步骤定义它:

index = pd.MultiIndex.from_tuples(zip(['metric_name', 'z_score', 'average', 'average'],
                         ['' ,'', 'A', 'B']))
df1.columns = index
print(df1)

  metric_name  z_score average   
                             A  B
0        None  1.18268       1  2
1        None  1.18268       1  2