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