Cyrine Ezzahra Cyrine Ezzahra - 2 months ago 6
Python Question

compute mean of a column with python

I have a dataframe df :

TIMESTAMP equipement1 equipement2
2016-05-10 13:20:00 0.000000 0.000000
2016-05-10 14:40:00 0.400000 0.500000
2016-05-10 15:20:00 0.500000 0.500000


I would like to compute for each equipmeentx the ratio when timestamp in [TS_min, TS_max]
For example function :

def ratio(df, 2016-05-10 14:40:00, 2016-05-10 15:20:00)
TIMESTAMP equipement1 equipement2
2016-05-10 14:40:00 0.4/(0.4+0.5) 0.5/(0.5+0.5)
2016-05-10 15:20:00 0.5/(0.4+0.5) 0.5/(0.5+0.5)


Any idea to help me please?

Thank you

Answer

Assuming TIMESTAMP is a datetime type, here is one way:

df = df.set_index('TIMESTAMP')
r = df.ix['2016-05-10 14:40:00':'2016-05-10 15:20:00']
r/r.sum()

                     equipement1  equipement2
TIMESTAMP
2016-05-10 14:40:00     0.444444          0.5
2016-05-10 15:20:00     0.555556          0.5
Comments