我想通过integer_transaction
汇总EMP_NAME
。
EMP_NAME
放在列而不是索引我想要输出
Emp_name Count Sum
a 2 1
b 1 0
import pandas as pd
import numpy as np
df = pd.DataFrame(data = {'EMP_NAME': ["a", "a", "b"], 'integer_transaction': [0, 1, 0]})
x=df.groupby(['EMP_NAME'])['integer_transaction'].agg({'Frequency_count': count, 'Frequency_Sum': np.sum})
x=df.groupby(['EMP_NAME'])['integer_transaction'].agg({'Frequency_count': np.size, 'Frequency_Sum': np.sum})
FutureWarning: using a dict on a Series for aggregation
is deprecated and will be removed in a future version
# -*- coding: utf-8 -*-
答案 0 :(得分:3)
尝试
df.groupby(['EMP_NAME'])['integer_transaction'].agg(["count", "sum"])
count sum
EMP_NAME
a 2 1
b 1 0
如果您确实需要,可以使用其他.rename("count": "Frequency_count", "sum": "Frequency_sum")
重命名列。
仅供参考,以下内容也完美无缺:
x=df.groupby(['EMP_NAME'])['integer_transaction'].agg({'Frequency_count': "count", 'Frequency_Sum': np.sum})
x
__main__:1: FutureWarning: using a dict on a Series for aggregation
is deprecated and will be removed in a future version
Out[26]:
Frequency_count Frequency_Sum
EMP_NAME
a 2 1
b 1 0
请注意引用count
的方式。
x=df.groupby(['EMP_NAME'])['integer_transaction'].agg({'Frequency_count': np.size, 'Frequency_Sum': np.sum})
x
__main__:1: FutureWarning: using a dict on a Series for aggregation
is deprecated and will be removed in a future version
Out[27]:
Frequency_count Frequency_Sum
EMP_NAME
a 2 1
b 1 0
您收到的警告只是告诉您将来会删除此功能,因此可能不会使用它们。但是,他们确实产生了正确的答案。
要将索引移至列,请尝试
df.groupby(['EMP_NAME'])['integer_transaction'].agg(["count", "sum"]).reset_index()
EMP_NAME count sum
0 a 2 1
1 b 1 0