将计算列添加到 pandas 中的数据透视表 table

Add a calculated column to a pivot table in pandas

您好,我正在尝试为多索引 pandas 主元 table 创建新列,以执行 countif 语句(类似于 excel),具体取决于索引的级别包含一个特定的字符串。这是示例数据:

df = pd.DataFrame({'City': ['Houston', 'Austin', 'Hoover','Adak','Denver','Houston','Adak','Denver'],
                   'State': ['Texas', 'Texas', 'Alabama','Alaska','Colorado','Texas','Alaska','Colorado'],
                   'Name':['Aria', 'Penelope', 'Niko','Susan','Aria','Niko','Aria','Niko'],
                   'Unit':['Sales', 'Marketing', 'Operations','Sales','Operations','Operations','Sales','Operations'],
                   'Assigned':['Yes','No','Maybe','No','Yes','Yes','Yes','Yes']},
                 columns=['City', 'State', 'Name', 'Unit','Assigned'])

pivot=df.pivot_table(index=['City','State'],columns=['Name','Unit'],values=['Assigned'],aggfunc=lambda x:', '.join(set(x)),fill_value='')

这是所需的输出(在屏幕截图中)。提前致谢!

尝试:

temp = pivot[('Mango',     'Aria',      'Sales')].str.len()>0
pivot['new col'] = temp.astype(int)

结果:

根据您的编辑:

import numpy as np
temp = pivot.xs('Sales', level=2, drop_level=False, axis = 1).apply(lambda x: np.sum([1 if y!='' else 0 for y in x]), axis = 1)
pivot[('',     'total sales',      'count how many...')]=temp