pandas Dataframe pandas

68bkxrlz  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(319)

我有一个csv:
messagereported_Agents Missing status flag已移除Host1 Missing status flag已升起Host1 Missing status flag已移除Host2 Missing status flag已升起Host2 Missing status flag已升起Host3
我需要得到这个
hosttotal MessagesMinsing flag removemissing flag RaisedHost1211Host22111Host3101
这就是我所做的,但它没有显示我需要什么。谢谢你的帮助

def load_data():
    event = pd.read_csv("events.csv")
    return event
data = load_data()

def GCmissing(data):
        x=data.groupby('hosts')['message'].value_counts()
3okqufwl

3okqufwl1#

使用 pandas.crosstab 然后沿列求和以获得总计:

out_df = pd.crosstab(index = df['reported_agents'], columns = df['message'])
out_df['total_messages'] = out_df.sum(axis = 1)

输出:

reported_agents Missing status flag was raised  Missing status flag was removed total_messages
host1           1                               1                               2
host2           1                               1                               2
host3           1                               0                               1

相关问题