【发布时间】:2014-11-03 01:49:00
【问题描述】:
我有一个带有日志数据的 pandas DataFrame:
host service
0 this.com mail
1 this.com mail
2 this.com web
3 that.com mail
4 other.net mail
5 other.net web
6 other.net web
我想在每台主机上找到错误最多的服务:
host service no
0 this.com mail 2
1 that.com mail 1
2 other.net web 2
我找到的唯一解决方案是按主机和服务分组,然后迭代 超过指数的 0 级。
谁能推荐一个更好、更短的版本?没有迭代?
df = df_logfile.groupby(['host','service']).agg({'service':np.size})
df_count = pd.DataFrame()
df_count['host'] = df_logfile['host'].unique()
df_count['service'] = np.nan
df_count['no'] = np.nan
for h,data in df.groupby(level=0):
i = data.idxmax()[0]
service = i[1]
no = data.xs(i)[0]
df_count.loc[df_count['host'] == h, 'service'] = service
df_count.loc[(df_count['host'] == h) & (df_count['service'] == service), 'no'] = no
【问题讨论】: