如何在numpy中总结一列

时间:2016-08-09 08:04:31

标签: python pandas numpy

我有一个数据帧,我在一个数组中转换(这是一个测试方案,因为我在pandas中的结果有问题)。现在我想总结一栏。

我有以下代码:

import sys
import pandas as pd
import numpy as np
import os
from tkinter import *


#data_rbu = np.genfromtxt('tmp_fakt_daten.csv', delimiter=',', dtype=None)
data_rbu = pd.read_excel('tmp_fakt_daten.xlsx')
array_rbu = data_rbu.as_matrix()
print(array_rbu)
summe1 = np.sum(array_rbu, axis=9, dtype=float)
print(summe1)

这是阵列!我想总结一下KW_WERT和NETTO_EURO。

FAK_ART,FAK_DAT,LEIST_DAT,KD_CRM,MW_BW,EQ_NR,MATERIAL,KW_WERT,NETTO_EURO,TA
ZPAF,2015-12-10,2015-12-31,T-HOME ICP,B,1001380363.0,B60ETS,0.15,18.9,SDH
ZPAF,2015-12-10,2015-12-31,T-HOME ICP,B,1001380363.0,B60ETS,0.145,18.27,SDH
ZPAF,2015-12-10,2015-12-31,T-HOME ICP,B,1001380363.0,B60ETS,0.145,18.27,SDH
ZPAF,2015-12-10,2015-12-31,T-HOME ICP,B,1001380363.0,B60ETS,0.15,18.9,SDH
ZPAF,2015-12-10,2015-12-31,T-HOME ICP,B,1001380363.0,B60ETS,0.15,18.9,SDH
ZPAF,2015-12-10,2015-12-31,T-HOME ICP,B,1001380363.0,B60ETS,0.145,18.27,SDH
ZPAF,2015-12-10,2015-12-31,T-HOME ICP,B,1001380363.0,B60ETS,0.15,18.9,SDH
ZPAF,2015-12-10,2015-12-31,T-HOME ICP,E,1001380594.0,B60ETS,3.011,252.92,DSLAM/MSAN

执行代码后,我收到此错误:

Traceback (most recent call last):
  File "C:\Users\A52113242\Desktop\PROJEKTE\[INPROGRESS] Faktura_sylvia\csv_einlesen bzgl. float\test2.py", line 12, in <module>
    summe1 = np.sum(array_rbu, axis=9, dtype=float)
  File "C:\Users\A52113242\AppData\Local\Downloaded Apps\Winpython\python-3.4.3\lib\site-packages\numpy\core\fromnumeric.py", line 1724, in sum
    out=out, keepdims=keepdims)
  File "C:\Users\A52113242\AppData\Local\Downloaded Apps\Winpython\python-3.4.3\lib\site-packages\numpy\core\_methods.py", line 32, in _sum
    return umr_sum(a, axis, dtype, out, keepdims)
ValueError: 'axis' entry is out of bounds

我明白问题是轴号...但我不知道我究竟做错了什么。我检查了numpy.sum ...

的文档

希望你能帮助我!

达米安

2 个答案:

答案 0 :(得分:1)

直接在熊猫中做到:

Byte[] bitmapData = Convert.FromBase64String(FixBase64ForImage(abc));
System.IO.MemoryStream streamBitmap = new System.IO.MemoryStream(bitmapData);
def = new Bitmap((Bitmap)Image.FromStream(streamBitmap));

答案 1 :(得分:1)

正如你所说的那些值在数组中:

In[10]:arr
Out[10]: 
array([['ZPAF', '2015-12-10', '2015-12-31', 'T-HOME ICP', 'B',
        1001380363.0, 'B60ETS', 0.15, 18.9, 'SDH'],
       ['ZPAF', '2015-12-10', '2015-12-31', 'T-HOME ICP', 'B',
        1001380363.0, 'B60ETS', 0.145, 18.27, 'SDH'],
       ['ZPAF', '2015-12-10', '2015-12-31', 'T-HOME ICP', 'B',
        1001380363.0, 'B60ETS', 0.145, 18.27, 'SDH'],
       ['ZPAF', '2015-12-10', '2015-12-31', 'T-HOME ICP', 'B',
        1001380363.0, 'B60ETS', 0.15, 18.9, 'SDH'],
       ['ZPAF', '2015-12-10', '2015-12-31', 'T-HOME ICP', 'B',
        1001380363.0, 'B60ETS', 0.15, 18.9, 'SDH'],
       ['ZPAF', '2015-12-10', '2015-12-31', 'T-HOME ICP', 'B',
        1001380363.0, 'B60ETS', 0.145, 18.27, 'SDH'],
       ['ZPAF', '2015-12-10', '2015-12-31', 'T-HOME ICP', 'B',
        1001380363.0, 'B60ETS', 0.15, 18.9, 'SDH'],
       ['ZPAF', '2015-12-10', '2015-12-31', 'T-HOME ICP', 'E',
        1001380594.0, 'B60ETS', 3.011, 252.92, 'DSLAM/MSAN']], dtype=object)

您可以使用arr.sum

sum_arr=arr.sum(axis=0)

axis=0它将逐列汇总,然后您可以根据其索引访问该列。对于列KW_WERTNETTO_EURO的情况,您可以得到以下总和:

In[25]:sum_arr[7]
Out[25]: 4.046

In[26]:sum_rr[8]
In[23]: 383.33