有办法让pandas的pd.crosstab默认包含margins=True吗?

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

Is there a way to make pandas pd.crosstab default to include margins=True?

问题

pd.crosstab在pandas中的默认行为如下,不包括边距:

`pandas.crosstab(index, columns, values=None, rownames=None, colnames=None, aggfunc=None, margins=False, margins_name='All', dropna=True, normalize=False)`

有没有办法改变pd.crosstab的默认行为?

我知道我们可以使用partialmethod来改变Python中的默认参数值。例如,要将pandas的sort_values更改为升序,我们可以这样做:

from functools import partialmethod
pd.DataFrame.sort_values = partialmethod(pd.DataFrame.sort_values, ascending=False)

我尝试过这样做:
pd.crosstab = partialmethod(pd.crosstab, margins=True)
但收到了这个错误:
TypeError: 'partialmethod' object is not callable

英文:

The default behavior of pd.crosstab in pandas is as follows, which does not include margins:

`pandas.crosstab(index, columns, values=None, rownames=None, colnames=None, aggfunc=None, margins=False, margins_name='All', dropna=True, normalize=False)`

Is there a way to change the default behavior of pd.crosstab?

I know we can use partialmethod to change the default parameter values in python. e.g. to change pandas sort_values to ascending, we can do

from functools import partialmethod
pd.DataFrame.sort_values = partialmethod(pd.DataFrame.sort_values, ascending=False)

I tried doing this:
pd.crosstab = partialmethod(pd.crosstab, margins=True)
but received this error:
TypeError: 'partialmethod' object is not callable

答案1

得分: 1

文档中:

> class functools.partialmethod(func, /, *args, **keywords)
> 返回一个新的partialmethod描述符,它的行为类似于partial,但它设计成用作方法定义,而不是直接可调用

这就是你遇到这个错误的原因。请尝试使用partial

from functools import partial
import pandas as pd
import numpy as np

a = np.array(["foo", "foo", "foo", "foo", "bar", "bar",
              "bar", "bar", "foo", "foo", "foo"], dtype=object)
b = np.array(["one", "one", "one", "two", "one", "one",
              "one", "two", "two", "two", "one"], dtype=object)
c = np.array(["dull", "dull", "shiny", "dull", "dull", "shiny",
              "shiny", "dull", "shiny", "shiny", "shiny"],
             dtype=object)

pd.crosstab = partial(pd.crosstab, margins=True)
print(pd.crosstab(a, [b, c], rownames=['a'], colnames=['b', 'c']))

输出:

b    one        two       All
c   dull shiny dull shiny    
a                            
bar    1     2    1     0   4
foo    2     2    1     2   7
All    3     4    2     2  11
英文:

From the documentation:

> class functools.partialmethod(func, /, *args, **keywords)
> Return a new partialmethod descriptor which behaves like partial except that it is designed to be used as a method definition rather than being directly callable.

That is why you are getting that error. Try partial instead:

from functools import partial
import pandas as pd
import numpy as np

a = np.array(["foo", "foo", "foo", "foo", "bar", "bar",
              "bar", "bar", "foo", "foo", "foo"], dtype=object)
b = np.array(["one", "one", "one", "two", "one", "one",
              "one", "two", "two", "two", "one"], dtype=object)
c = np.array(["dull", "dull", "shiny", "dull", "dull", "shiny",
              "shiny", "dull", "shiny", "shiny", "shiny"],
             dtype=object)

pd.crosstab = partial(pd.crosstab, margins=True)
print(pd.crosstab(a, [b, c], rownames=['a'], colnames=['b', 'c']))

Output:

b    one        two       All
c   dull shiny dull shiny    
a                            
bar    1     2    1     0   4
foo    2     2    1     2   7
All    3     4    2     2  11

huangapple
  • 本文由 发表于 2023年8月5日 03:54:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838816.html
匿名

发表评论

匿名网友

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

确定