从 pandas DataFrame 中提取单个类别的计数

Extract count of single category from a pandas DataFrame

我目前有一个 DataFrame,其中包含有关从一个职位发送到另一个职位的电子邮件的信息。

      fromJobtitle         toJobtitle  e-mails
0              CEO                CEO       65
1              CEO           Director       23
2              CEO           Employee       56
3              CEO    In House Lawyer        7
4              CEO            Manager      104
..             ...                ...      ...
87  Vice President  Managing Director      112
88  Vice President          President      385
89  Vice President             Trader       78
90  Vice President            Unknown     1088
91  Vice President     Vice President     2304

我正在寻找一种方法,以便可以获取每个职位的总数。 示例输出为:

        totalJobtitle       e-mails
0                 CEO           670
1   Managing Director          2341
2      Vice President          4720
3            Employee          3560
4              Trader           250

我可以使用的一个小例子

d = {'fromJobtitle': ["CEO", "CEO","VicePresident","VicePresident"], 'mail': [3, 4, 5, 6 ]}
df = pd.DataFrame(data=d)

df:

    fromJobtitle    mail
0   CEO 3
1   CEO 4
2   VicePresident   5
3   VicePresident   6

现在这个:

 df = pd.pivot_table(df, index=['fromJobtitle'],values=['mail'],aggfunc=np.sum)

df:

fromJobtitle    mail    
CEO 7
VicePresident   11

函数来源: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.pivot_table.html