使用Pandas Python中的多索引数据透视表对列值求和

时间:2018-02-19 18:16:51

标签: python pandas dataframe pivot-table

我有这样的数据

cmin

我想将数量与多索引数据透视表相加,使其看起来如下所示。这是link我正在关注但无法获得正确的结果

enter image description here

需要你的帮助。

1 个答案:

答案 0 :(得分:6)

这是一种方式。



            /*
             * To change this license header, choose License Headers in Project Properties.
             * To change this template file, choose Tools | Templates
             * and open the template in the editor.
             */
            package edu.emit.project.view.shape;

            import javafx.scene.shape.Polygon;
            import javafx.scene.shape.QuadCurve;

            /**
             *
             * @author heniroger
             */

            public class Pointe extends Polygon{
                private final Pointe context = this;
                Double[] points = {0.0, 10.0, -10.0, -10.0, 10.0, -10.0};
                private QuadCurve lien;
                private Double radius;

                public Pointe() {
                    this.getPoints().addAll(points);

                }
                public void linkTo(QuadCurve lien,double sommetRadius){
                    this.lien = lien;
                    this.radius = sommetRadius * 1.25;
                    initialize();

                }

                private void initialize() {
                    double angle = Math.atan2(lien.getEndY() - lien.getStartY(), lien.getEndX() - lien.getStartX()) * 180 / 3.14;

                    double height = lien.getEndY() - lien.getStartY();
                    double width = lien.getEndX() - lien.getStartX();
                    double length = Math.sqrt(Math.pow(height, 2) + Math.pow(width, 2));

                    double subtractWidth = radius * width / length;
                    double subtractHeight = radius * height / length;

                    setRotate(angle - 90);
                    setTranslateX(lien.getStartX());
                    setTranslateY(lien.getStartY());
                    setTranslateX(lien.getEndX()- subtractWidth);
                    setTranslateY(lien.getEndY() - subtractHeight);

                }

                public void update(){
                    initialize();
                }



            }


实际代码:

import pandas as pd
import io
import json

s = '''\
Employed    Coverage    Education   Amount
No          Basic       Bachelor    541.8029122
No          Extended    Bachelor    312.6400955
No          Premium     Bachelor    427.9560121
No          Basic       Bachelor    91.17931022
No          Basic       Bachelor    533.6890081
Yes         Basic       Bachelor    683.484326
Yes         Basic       College     586.2670885
No          Premium     Master      725.0412884
Yes         Basic       Bachelor    948.3628611'''

# Recreate the dataframe
df = pd.read_csv(io.StringIO(s), sep='\s+')

注意:

  • 转换为类别可确保报告所有方案 系列。
  • 数据透视表默认计算为df['Coverage'] = df['Coverage'].astype('category') pd.pivot_table(df, index='Education', columns=['Employed', 'Coverage'], values='Amount', aggfunc='sum', fill_value=0) # Employed No Yes # Coverage Basic Extended Premium Basic Extended Premium # Education # Bachelor 1166.671231 312.640096 427.956012 1631.847187 0.0 0.0 # College 0.000000 0.000000 0.000000 586.267088 0.0 0.0 # Master 0.000000 0.000000 725.041288 0.000000 0.0 0.0 ,因此必须明确指定mean