应用高亮于数据透视表

huangapple go评论144阅读模式
英文:

Apply highlight to pivot_table

问题

我通过pivot_table函数获取DataFrame:

  1. df2 = pd.pivot_table(df, values=['labor costs'],
  2. index=['Division', 'Performer'],
  3. columns=['completed on time'], aggfunc=[np.sum, len], margins=True, fill_value=0)

如何根据条件突出显示行:on schedule == 0 and overdue == 0,就像上面的表格一样?

我这样做:

  1. def apply_colors(df_slice: pd.DataFrame) -> pd.DataFrame:
  2. styles_df = pd.DataFrame('', index=df_slice.index, columns=df_slice.columns)
  3. print('df_slice.index', df_slice.index)
  4. print('df_slice.columns', df_slice.columns)
  5. styles_df['Performer'] = np.select([
  6. # 条件 1
  7. df_slice['overdue'] == 0 & df_slice['on schedule'] == 0
  8. ], [
  9. # 条件 1 的颜色
  10. 'background-color: silver',
  11. ])
  12. return styles_df
  13. df2.style.apply(apply_colors, axis=None)

然后出现了 KeyError: 'overdue'。

  1. #print('df_slice.columns', df_slice.columns)
  2. df_slice.columns MultiIndex([('sum', 'labor costs', 'on time'),
  3. ('sum', 'labor costs', 'on schedule'),
  4. ('sum', 'labor costs', 'overdue'),
  5. ('len', 'labor costs', 'on time'),
  6. ('len', 'labor costs', 'on schedule'),
  7. ('len', 'labor costs', 'overdue')],
  8. names=[None, None, 'completed on time'])
英文:

I get DataFrame by function pivot_table:

  1. df2 = pd.pivot_table(df, values=['labor costs],
  2. index=['Division', 'Perfomer'],
  3. columns=['completed on time'], aggfunc=[np.sum, len], margins=True, fill_value=0)

应用高亮于数据透视表
How can I highlight the row by condition: on schedule == 0 and overdue == 0 like table above?

I do:

  1. def apply_colors(df_slice: pd.DataFrame) -> pd.DataFrame:
  2. styles_df = pd.DataFrame('', index=df_slice.index, columns=df_slice.columns)
  3. print('df_slice.index', df_slice.index)
  4. print('df_slice.columns', df_slice.columns)
  5. styles_df['Perfomer'] = np.select([
  6. # Condition 1
  7. df_slice['overdue'] == 0 & df_slice['on schedule'] == 0
  8. ], [
  9. # Color for Condition 1
  10. 'background-color: silver',
  11. ])
  12. return styles_df
  13. df2.style.apply(apply_colors, axis=None)

And get: KeyError: 'overdue'

  1. #print('df_slice.columns', df_slice.columns)
  2. df_slice.columns MultiIndex([('sum', 'labor costs', 'on time'),
  3. ('sum', 'labor costs', 'on schedule'),
  4. ('sum', 'labor costs', 'overdue'),
  5. ('len', 'labor costs', 'on time'),
  6. ('len', 'labor costs', 'on schedule'),
  7. ('len', 'labor costs', 'overdue'),
  8. names=[None, None, 'completed on time'])

答案1

得分: 1

我认为你需要在 DataFrame.any 函数中添加括号,用于测试是否至少有一个匹配,将 default 参数添加到 numpy.select 函数中,以便在未匹配的情况下添加空格,用 numpy.broadcast_to 函数来重复着色:

  1. def apply_colors(df_slice: pd.DataFrame) -> pd.DataFrame:
  2. arr = np.select([
  3. # 条件 1
  4. ((df_slice.xs('overdue', axis=1, level=2) == 0) &
  5. (df_slice.xs('on schedule', axis=1, level=2) == 0)).any(axis=1)
  6. ], [
  7. # 条件 1 的颜色
  8. 'background-color: silver',
  9. ], default='')
  10. return pd.DataFrame(np.broadcast_to(arr[:, None], df_slice.shape),
  11. index=df_slice.index,
  12. columns=df_slice.columns)
英文:

I think you need add parantheses with DataFrame.any for test at least one match, default parameter to numpy.select for space if not matched masks, for repeat coloring is used numpy.broadcast_to:

  1. def apply_colors(df_slice: pd.DataFrame) -> pd.DataFrame:
  2. # print('df_slice.index', df_slice.index)
  3. # print('df_slice.columns', df_slice.columns)
  4. arr = np.select([
  5. # Condition 1
  6. ((df_slice.xs('overdue', axis=1, level=2) == 0) &
  7. (df_slice.xs('on schedule', axis=1, level=2) == 0)).any(axis=1)
  8. ], [
  9. # Color for Condition 1
  10. 'background-color: silver',
  11. ], default='')
  12. return pd.DataFrame(np.broadcast_to(arr[:, None], df_slice.shape),
  13. index=df_slice.index,
  14. columns=df_slice.columns)

huangapple
  • 本文由 发表于 2023年8月10日 15:53:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定