pivot_table在省略列时不生成边距。

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

pivot_table makes no margins when omitting columns

问题

以下是您要翻译的内容:

df = pd.DataFrame([[1,2,9],[3,4,6], [5,6,8]]) 
df.columns = ["a", "b", "c"]              

# no margins despite margins=True:
pd.pivot_table(data = df, margins = True, columns=[], values=["a", "c"], aggfunc=["sum"], index=["b"]) 

# margins as usual:
pd.pivot_table(data = df, margins = True, columns=["c"], values=["a"], aggfunc=["sum"], index=["b"])

文档中没有提到没有列时不起作用,所以我期望如此。当然,我可能会漏掉一些信息。

编辑
出于格式原因,排除了df的结果。

英文:

Example:

df = pd.DataFrame([[1,2,9],[3,4,6], [5,6,8]]) 
df.columns = ["a", "b", "c"]              

# no margins despite margins=True:
pd.pivot_table(data = df, margins = True, columns=[], values=["a", "c"], aggfunc=["sum"], index=["b"]) 

# margins as usual:
pd.pivot_table(data = df, margins = True, columns=["c"], values=["a"], aggfunc=["sum"], index=["b"])

The documentation does not say margins don't work without columns, so I expect that. Of course I might miss information.

EDIT
excluded df results for formatting reasons

答案1

得分: 3

以下是您要翻译的内容:

"The margin is here, it just doesn't have a 'All' name. It consists of the original 'a' Series tranformed by the aggregation function.

To see it for yourself, let's change the aggregation function to return the sum + 10:

def f(x):
    return sum(x)+10

pd.pivot_table(data = df, margins = True, columns=[], values=['a'],
               aggfunc=[f], index=['b'])

Output:

      f
      a
b      
2    11  # sum([1])+10
4    13
6    15
All  19  # sum([1, 3, 5])+10

With two columns:

pd.pivot_table(data = df, margins = True, columns=['b'],
               values=['a', 'c'], aggfunc=[f], index=['b'])

        f                                      
        a                     c                
b       2     4     6 All     2     4     6 All
b                                              
2    11.0   NaN   NaN  11  19.0   NaN   NaN  19
4     NaN  13.0   NaN  13   NaN  16.0   NaN  16
6     NaN   NaN  15.0  15   NaN   NaN  18.0  18
All  11.0  13.0  15.0  19  19.0  16.0  18.0  33

#      ↑     ↑     ↑         ↑     ↑     ↑
# those are absent with columns=[]
# because there is nothing to pivot

pd.pivot_table(data = df, margins = True, columns=[],
               values=['a', 'c'], aggfunc=[f], index=['b'])

      f    
      a   c
b          
2    11  19
4    13  16
6    15  18
All  19  33
英文:

The margin is here, it just doesn't have a 'All' name. It consists of the original "a" Series tranformed by the aggregation function.

To see it for yourself, let's change the aggregation function to return the sum + 10:

def f(x):
    return sum(x)+10

pd.pivot_table(data = df, margins = True, columns=[], values=["a"],
               aggfunc=[f], index=["b"])

Output:

      f
      a
b      
2    11  # sum([1])+10
4    13
6    15
All  19  # sum([1, 3, 5])+10

With two columns:

pd.pivot_table(data = df, margins = True, columns=['b'],
               values=["a", "c"], aggfunc=[f], index=["b"])

        f                                      
        a                     c                
b       2     4     6 All     2     4     6 All
b                                              
2    11.0   NaN   NaN  11  19.0   NaN   NaN  19
4     NaN  13.0   NaN  13   NaN  16.0   NaN  16
6     NaN   NaN  15.0  15   NaN   NaN  18.0  18
All  11.0  13.0  15.0  19  19.0  16.0  18.0  33

#      ↑     ↑     ↑         ↑     ↑     ↑
# those are absent with columns=[]
# because there is nothing to pivot

pd.pivot_table(data = df, margins = True, columns=[],
               values=["a", "c"], aggfunc=[f], index=["b"])

      f    
      a   c
b          
2    11  19
4    13  16
6    15  18
All  19  33

huangapple
  • 本文由 发表于 2023年6月15日 16:28:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480565.html
匿名

发表评论

匿名网友

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

确定