英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论