【问题标题】:How to apply formula to only certain rows of pivot table in Pandas?如何将公式仅应用于 Pandas 中的某些数据透视表行?
【发布时间】:2018-07-29 15:07:58
【问题描述】:

我拥有的原始df:

    year security_software  usage
0   2006        anti_virus     77
1   2006          firewall     64
2   2006  security_updates     59
3   2007        anti_virus     84
4   2007          firewall     71
5   2007  security_updates     52
6   2008        anti_virus     91
7   2008          firewall     81
8   2008  security_updates     63
9   2009        anti_virus     90
10  2009          firewall     83
11  2009  security_updates     61
12  2010        anti_virus     90
13  2010          firewall     85
14  2010  security_updates     67
15  2011        anti_virus     89
16  2011          firewall     84
17  2011  security_updates     61
18  2012        anti_virus     83
19  2012          firewall     72
20  2012  security_updates     52
21  2013        anti_virus     84
22  2013          firewall     71
23  2013  security_updates     75
24  2014        anti_virus     81
25  2014          firewall     69
26  2014  security_updates     79
27  2015        anti_virus     80
28  2015          firewall     61
29  2015  security_updates     79

我为我的数据创建了一个数据透视表,并为它添加了一个“总计”行,其中包含以下命令。

sec_pivot = df.pivot(index="security_software",columns='year',values = "usage")
sec_pivot.loc['Total'] = sec_pivot.sum()
print(sec_pivot)

year security_software  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015
0           anti_virus    77    84    91    90    90    89    83    84    81    80
1             firewall    64    71    81    83    85    84    72    71    69    61
2     security_updates    59    52    63    61    67    61    52    75    79    79

如何在不影响“总计”行本身的情况下将所有使用汇总更改为总值的百分比?

【问题讨论】:

  • 是原来的df吗?没有列year 也没有usage
  • 我很抱歉。现在是正确的,非常感谢。

标签: python pandas pivot pivot-table percentage


【解决方案1】:

您可以将loc 与索引切片一起使用

sec_pivot.loc["anti_virus":"security_updates"]/sec_pivot.loc["Total"] * 100

year                2006    2007        2008    
security_software           
anti_virus          38.5    40.579710   38.723404
firewall            32.0    34.299517   34.468085
security_updates    29.5    25.120773   26.808511

如果要替换原始df中的值,只需分配回

sec_pivot.loc["anti_virus":"security_updates"] = sec_pivot.loc["anti_virus":"security_updates"]/sec_pivot.loc["Total"] * 100



year                2006    2007        2008    
security_software           
anti_virus          38.5    40.579710   38.723404
firewall            32.0    34.299517   34.468085
security_updates    29.5    25.120773   26.808511
Total              200.0    207.000000  235.000000

如果不在索引中,会稍微复杂一些,但不会太多:

sec_pivot.loc[sec_pivot.index != 'Total', 1:] = sec_pivot[sec_pivot.index != 'Total'].iloc[:,1:]/sec_pivot.iloc[:,1:].loc["Total"] * 100

注意1: 表示从第 1 列到最后。我考虑你的年份从列索引 1 开始(2006 年)。如果您的df 中有其他列,请将1: 更改为a:b,其中a 在您的第一年列的索引中,b 是您去年列的索引

【讨论】:

  • 感谢您的回答。如果数据透视表有 reset_index(),我们如何更改它? sec_pivot = df.pivot(index="security_software",columns='year',values = "usage").reset_index()
  • 我已经尝试了 reset_index() 的代码,但是字符串列变成了 NaN。 security_software NaN NaN NaN NaN anti_virusfirewallsecurity_updates
  • @Venice 您是否在左侧指定为sec_pivot.loc[sec_pivot.index != 'Total', 1:] = ?
  • 啊!我错过了1:。非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多