我在pandas中创建了聚合函数并保存结果:
import pandas as pd
_dwh = df2_date[df2_date.STATUS == 'A']
.groupby('Party_id')
.DURATION_DWH.agg(np.mean)
结果如下所示:
然后,我尝试按如下方式切换到pandas DataFrame
:
df2_dwh = pd.DataFrame(_dwh)
它返回了一些令人困惑的结果:
如何创建包含1,...,n
和Party_id
以及Duration_DWH
等索引的普通DataFrame。
由于
答案 0 :(得分:1)
您需要添加参数as_index=False
或reset_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