系列到DataFrame Pandas转换期间的误导性索引

时间:2017-02-14 14:36:00

标签: python pandas

我在pandas中创建了聚合函数并保存结果:

import pandas as pd
_dwh = df2_date[df2_date.STATUS == 'A']
       .groupby('Party_id')
       .DURATION_DWH.agg(np.mean)

结果如下所示:

enter image description here

然后,我尝试按如下方式切换到pandas DataFrame

df2_dwh = pd.DataFrame(_dwh)

它返回了一些令人困惑的结果:

enter image description here

如何创建包含1,...,nParty_id以及Duration_DWH等索引的普通DataFrame。

由于

1 个答案:

答案 0 :(得分:1)

您需要添加参数as_index=Falsereset_index

_dwh=df2_date[df2_date.STATUS=='A'].groupby('Party_id', as_index=False).DURATION_DWH.mean()

print (_dwh)
                 Party_id  DURATION_DWH
0  214BB440D604466275DFBB         574.0
1  214BB440D604466276D1B3         574.0
2  214BB440D604466371D1B2         558.5
3  214BB440D604466371DDB1         578.0
4  214BB440D604466373DBB5         578.0
_dwh=df2_date[df2_date.STATUS=='A'].groupby('Party_id', as_index=False).DURATION_DWH
                                   .mean()
                                   .reset_index()

print (_dwh)

                     Party_id  DURATION_DWH
    0  214BB440D604466275DFBB         574.0
    1  214BB440D604466276D1B3         574.0
    2  214BB440D604466371D1B2         558.5
    3  214BB440D604466371DDB1         578.0
    4  214BB440D604466373DBB5         578.0