2

我有一个数据文件,我想在其中按列求和。

数据看起来像:

Date;Heure;U12_min;U12_max;U12_moy
13/07/12-15:10:02;581072;41719;42058;41892
13/07/12-15:20:01;581088;41610;42108;41897
13/07/12-15:30:01;581105;41810;42048;41931
13/07/12-15:40:01;581122;41483;41988;41798
...
14/07/12-00:00:01;581955;42013;42377;42210
14/07/12-00:10:01;581972;41502;42117;41851
14/07/12-00:20:01;581988;41477;42349;42135
14/07/12-00:30:00;582005;42092;42517;42264
14/07/12-00:40:00;582022;41766;42653;42032 
14/07/12-00:50:01;582038;41836;42267;42069
14/07/12-01:00:01;582055;42056;42334;42214

这是我的代码:

from pandas import *

fich = 'D://ENERGIE//test2.csv'
df = read_csv(fich, delimiter=";", index_col="Date")
df.sum["Heure"]
print df

我收到以下消息

df.sum["Heure"]
TypeError: 'instancemethod' object is not subscriptable
4

3 回答 3

4

pandas 的 groupby 函数不应该与 sum 一起使用吗?

from pandas import *

fich = 'D://ENERGIE//test2.csv'
df = read_csv(fich, delimiter=";", index_col="Date")
sumValue = df.groupby("Heure").sum()
print sumValue 
于 2012-07-18T10:00:21.147 回答
1

或者,没有pandas,该csv模块非常方便:

import csv
fich = 'D://ENERGIE//test2.csv'

with open(fich) as f:
    d = csv.DictReader(f, delimiter=';')
    print sum(float(row['Heure']) for row in d)
于 2012-07-18T10:09:29.057 回答
1

如果要对“Heure”列中的所有值求和,只需执行以下操作:

$> df.Heure.sum()
于 2012-07-18T10:07:04.693 回答