0

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

Poisson
  • 1,543
  • 6
  • 23
  • 34
  • Possible duplicate of [pandas get column average](http://stackoverflow.com/questions/31037298/pandas-get-column-average) – Jann Sep 08 '16 at 18:33

1 Answers1

1

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
Zeugma
  • 31,231
  • 9
  • 69
  • 81
  • Thank you Boud, but why we have to set as index TIMESTAMP? – Poisson Sep 08 '16 at 17:48
  • 1
    Because it's very easy and powerful to work with datetimeindex in pandas. http://pandas.pydata.org/pandas-docs/stable/timeseries.html – Zeugma Sep 08 '16 at 17:57