我有一个数据框,可以在数组中进行转换(这是一个测试方案,因为我对熊猫的结果有疑问)。现在我想总结一栏。
我有以下代码:
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的文档...
希望你能帮我!
达米安
最佳答案
正如您所说的,这些值在数组中:
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_WERT
和NETTO_EURO
列,您可以将总和作为:In[25]:sum_arr[7]
Out[25]: 4.046
In[26]:sum_rr[8]
In[23]: 383.33