pandas 如何总结numpy中的一列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38845265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to sum up a column in numpy
提问by Damian
i got a dataframe which i convert in an array (that is a testscenario because i have problems with the results in pandas). Now i want to sum up one column.
我得到了一个数据帧,我将它转换成一个数组(这是一个测试场景,因为我在 Pandas 中的结果有问题)。现在我想总结一栏。
I have the following code:
我有以下代码:
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)
This is the Array! I want to sum up KW_WERT and NETTO_EURO.
这就是阵法!我想总结一下 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
After executing the code i get this error:
执行代码后,我收到此错误:
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
I understand that the problem is the axis number.. but i dont know what im exactly doing wrong. I checked the documentation for numpy.sum...
我知道问题是轴号..但我不知道我到底做错了什么。我检查了 numpy.sum 的文档...
Hope you can help me!
希望你能帮我!
Damian
达米安
采纳答案by shivsn
As you said the values are in array:
正如你所说,值在数组中:
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)
you can do using arr.sum
:
你可以使用arr.sum
:
sum_arr=arr.sum(axis=0)
axis=0
it will sum column wise,then you can access the column based on its index.In your case for columns KW_WERT
and NETTO_EURO
you can get the sum as:
axis=0
它将按列求和,然后您可以根据其索引访问该列。在您的列的情况下KW_WERT
,NETTO_EURO
您可以得到总和为:
In[25]:sum_arr[7]
Out[25]: 4.046
In[26]:sum_rr[8]
In[23]: 383.33
回答by Julien
do it directly in pandas:
直接在Pandas中做到这一点:
data_rbu = pd.read_excel('tmp_fakt_daten.xlsx')
summe1 = data_rbu['KW_WERT'] + data_rbu['NETTO_EURO'] # gets you a series
summe1.sum() # gets you the total sum (if that's what you are after)