列大熊猫的百分比

时间:2015-11-29 17:09:37

标签: python pandas

我有一张非常基本的表格:

/**
 * No Predictive Animations GridLayoutManager
 */
private static class NpaGridLayoutManager extends GridLayoutManager {
    /**
     * Disable predictive animations. There is a bug in RecyclerView which causes views that
     * are being reloaded to pull invalid ViewHolders from the internal recycler stack if the
     * adapter size has decreased since the ViewHolder was recycled.
     */
    @Override
    public boolean supportsPredictiveItemAnimations() {
        return false;
    }

    public NpaGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public NpaGridLayoutManager(Context context, int spanCount) {
        super(context, spanCount);
    }

    public NpaGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
        super(context, spanCount, orientation, reverseLayout);
    }
}

我可以使用以下方法创建一个基本数据透视表:

    gov dis value1  value2
0   a   a_1 8   8
1   a   a_2 7   18
2   a   a_3 3   2
3   a   a_4 12  12
4   b   b_1 4   11
5   b   b_2 16  9.....

创建:

 t = pd.pivot_table(per_t,index=["gov"],values=['value1'],aggfunc=[np.mean])

我想将此转换为列的百分比,并且无法找到有关如何执行此操作的任何明确内容?有没有办法改变aggfunc来计算列百分比?

1 个答案:

答案 0 :(得分:3)

您可以除以总和:

In [11]: t.sum()
Out[11]:
mean  value1    17.5
dtype: float64

In [12]: t / t.sum()
Out[12]:
         mean
       value1
gov
a    0.428571
b    0.571429

In [13]: (t / t.sum()) * 100
Out[13]:
          mean
        value1
gov
a    42.857143
b    57.142857

注意:我认为你必须在枢轴之后执行此操作,因为聚合功能无法访问其他组。