且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

pandas.crosstab中缺少数据

更新时间:2023-02-02 21:49:09

我认为没有办法做到这一点,并且crosstab在源代码中调用pivot_table,但似乎没有提供此功能任何一个. 我提出了一个问题,此处.

I don't think there is a way to do this, and crosstab calls pivot_table in the source, which doesn't seem to offer this either. I raised it as an issue here.

一种骇人的解决方法(可能与您正在使用的相同...):

A hacky workaround (which may or may not be the same as you were already using...):

from itertools import product
ct = pd.crosstab(a, [b, c], rownames=['a'], colnames=['b', 'c'])
a_x_b = list(product(np.unique(b), np.unique(c)))
a_x_b = pd.MultiIndex.from_tuples(a_x_b)

In [15]: ct.reindex_axis(a_x_b, axis=1).fillna(0)
Out[15]:
      one          two
     dull  shiny  dull  shiny
a
bar     1      0     1      0
foo     2      0     1      2

如果product太慢,则是它的一个numpy实现.

If product is too slow, here is a numpy implementation of it.