简体   繁体   中英

Howto sum by col

I have a datafile in which i would like to do a sum by column.

The data looks like :

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

Here is my code :

from pandas import *

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

and i get the following message

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

Shouldn't the groupby function of pandas work with sum ?

from pandas import *

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

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

$> df.Heure.sum()

Or, without pandas , the csv module is pretty handy:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM