1 回答
TA贡献2051条经验 获得超10个赞
用于DataFrame.loc
按条件和列名过滤,然后用于避免MultiIndex
添加DataFrame.reset_index
或参数as_index=False
:
df = df.loc[df['timeperiod']>0, ['id', 'timeperiod','timeperiodtype','good']].groupby(['id','timeperiod','timeperiodtype']).mean().reset_index()
或者:
df = df.loc[df['timeperiod']>0, ['id', 'timeperiod','timeperiodtype','good']].groupby(['id','timeperiod','timeperiodtype'], as_index=False).mean()
print(df)
id timeperiod timeperiodtype good
0 0 1 m 0.6
1 0 6 m 0.4
2 0 12 m 1.0
3 1 1 m 0.6
4 1 6 m 0.4
5 1 12 m 1.0
print(df[df['timeperiod']>6])
id timeperiod timeperiodtype good
2 0 12 m 1.0
5 1 12 m 1.0
编辑:
对于过滤器,MuiltiIndex
可以使用Index.get_level_values
:
df = df.loc[df['timeperiod']>0, ['id', 'timeperiod','timeperiodtype','good']].groupby(['id','timeperiod','timeperiodtype']).mean()
print(df)
good
id timeperiod timeperiodtype
0 1 m 0.6
6 m 0.4
12 m 1.0
1 1 m 0.6
6 m 0.4
12 m 1.0
print(df[df.index.get_level_values('timeperiod')>6])
good
id timeperiod timeperiodtype
0 12 m 1.0
1 12 m 1.0
添加回答
举报