如何将系列索引转换为多索引

时间:2018-02-05 15:49:38

标签: python pandas dataframe matplotlib series

我需要从python系列数据中绘制条形图,该系列是从以下数据框中获取的: raw_data.groupby('Age_group')['Survived'].mean() `

the result of above code is below:
Age_group  
(0, 10]     0.593750
(10, 20]    0.382609
(20, 30]    0.334152
(30, 40]    0.445161
(40, 50]    0.383721
(50, 60]    0.404762
(60, 70]    0.235294
(70, 80]    0.200000
Name: Survived, dtype: float64

I want it to reindex to a multi index series like as below, so that I when I draw the bar chart it will show y-axis label as the mean number:

Age_group   Mean
(0, 10]     0.593750
(10, 20]    0.382609
(20, 30]    0.334152
(30, 40]    0.445161
(40, 50]    0.383721
(50, 60]    0.404762
(60, 70]    0.235294
(70, 80]    0.200000
Name: Survived, dtype: float64

`

为了补充上述问题,我实际上得到了下面的另一个多指数系列,它可以被取消堆积并绘制成条形图:

raw_data.groupby(['Age_group', 'Survived']).size() `

Age_group  Survived
(0, 10]    0            26
           1            38
(10, 20]   0            71
           1            44
(20, 30]   0           271
           1           136
(30, 40]   0            86
           1            69
(40, 50]   0            53
           1            33
(50, 60]   0            25
           1            17
(60, 70]   0            13
           1             4
(70, 80]   0             4
           1             1
dtype: int64
In [550]:

raw_data.groupby(['Age_group','Survived'])。size()。unstack()。plot(kind ='bar',stacked = False)`

2 个答案:

答案 0 :(得分:0)

这是你需要的吗?

s=raw_data.groupby(['Age_group', 'Survived']).size()

s.div(s.sum(level=0),level=0).unstack().plot(kind='bar', stacked=False)

Update1

s.div(s.sum(level=0),level=0).to_frame('mean').reset_index('Survived').set_index('mean',append=True)

UPDATE2

s
Out[778]: 
Age_group
(0,10]     0.593750
(10,20]    0.382609
(20,30]    0.334152
(30,40]    0.445161
(40,50]    0.383721
(50,60]    0.404762
(60,70]    0.235294
(70,80]    0.200000
Name: Mean, dtype: float64


s.index=pd.MultiIndex.from_arrays([s.index,s])
s
Out[783]: 
Age_group  Mean    
(0,10]     0.593750    0.593750
(10,20]    0.382609    0.382609
(20,30]    0.334152    0.334152
(30,40]    0.445161    0.445161
(40,50]    0.383721    0.383721
(50,60]    0.404762    0.404762
(60,70]    0.235294    0.235294
(70,80]    0.200000    0.200000
Name: Mean, dtype: float64

答案 1 :(得分:0)

你需要的IIUC:

raw_data.groupby(['Age_group', 'Survived'])['Survived']
        .mean()
        .unstack()
        .plot(kind='bar', stacked=False)

编辑:你不需要MultiIndex

raw_data.groupby('Age_group')['Survived'].mean().plot(kind='bar', stacked=False)