如何在python中总结N列?

时间:2018-02-12 14:53:42

标签: python pandas dataframe

我是一只大熊猫df,我想总结N列。 df可能如下所示:

A B C D ... X

1 4 2 6     3
2 3 1 2     2 
3 1 1 2     4
4 2 3 5 ... 1

我想得到这样的df:

A Z

1 15
2 8
3 8
4 11

A变量不是索引,而是变量。

2 个答案:

答案 0 :(得分:5)

Series所有列sum创建的新A使用join,而不是df = df[['A']].join(df.drop('A', 1).sum(axis=1).rename('Z'))

A

首先按pop提取列df = df.pop('A').to_frame().join(df.sum(axis=1).rename('Z'))

df = df.iloc[:, [0]].join(df.iloc[:, 1:].sum(axis=1).rename('Z'))

如果要按位置选择列,请使用iloc

print (df)
   A   Z
0  1  15
1  2   8
2  3   8
3  4  11
      _displayChart: function(label1, label2){
    var timeFormat = 'DD/MM/YYYY HH:mm';

    var ctx = document.getElementById(this.heading).getContext('2d');

    const data = {
        // Labels should be Date objects
        //labels:this._stats[1],
        datasets: [{
            fill: false,
            label: label1,
            data: this._stats[2],
            borderColor: '#fe8b36',
            pointRadius: 0,
            backgroundColor: [
            '#fe8b36'
            ]
        },{
            label: label2,
            fill: false,
            data: this._stats[3],
            borderWidth: 1,
            pointRadius: 0,
            borderColor: 'rgba(99, 100, 200, 1)',
            backgroundColor: [
            'rgba(99, 100, 200, 0.2)'
            ]
          }]
    }
    const options = {
        type: 'line',
        data: data,
        options: {
            responsive: false,
            fill: false,
            responsive: true,
            maintainAspectRatio:!this._isMobile(),
            max: this.max,
            hover: {
                // Overrides the global setting
                mode: 'index'
            },
            annotation: {
              drawTime: 'beforeDatasetsDraw',
              events: ['click'],
              annotations: []
            },
            legend: {
              labels: {
                filter: function(legendItem, chartData) {
                  if(legendItem.datasetIndex == 2 || legendItem.datasetIndex == 3 ){
                    return false;
                  }
                  return true;
                }
              }
            },
            tooltips: {
              mode:'index',
              intersect : false,
              callbacks: {
                title: function(tooltipItems, data) {
                  return tooltipItems[0].xLabel.format(timeFormat);
                }, 
                label: function(tooltipItems, data) {
                  return tooltipItems.yLabel+'%';
                }
              }
            },
            scales: {
                xAxes: [{
                    type: 'time',
                    display: true,
                    time:{
                      unit:'day',
                      min:moment(Date.now()-this.monthTs),
                      max:this._stats[1][this._stats[1].length]
                    }
                }],
                yAxes: [{
                    ticks: {
                        beginAtZero: true,
                    },
                    display: true
                }]
            }
        }
    }
    this._chart = new Chart(ctx, options);
  },

答案 1 :(得分:1)

您可以groupby

df.groupby(['A']+['Z']*(df.shape[1]-1),axis=1).sum()
Out[252]: 
   A   Z
0  1  15
1  2   8
2  3   8
3  4  11