Cyrine Ezzahra Cyrine Ezzahra - 3 months ago 7
Python Question

mean of the probability activation for each day of a week with python

I have a dataframe df with this structure :

TIMESTAMP probab-activ1 probab-activ3 probab-activ5
2015-07-31 23:00:00 90.0 90.0 90.0
2015-07-31 23:10:00 0.0 0.0 0.0
2015-07-31 23:20:00 0.0 0.0 0.0
2015-07-31 23:30:00 0.0 0.0 0.0
2015-07-31 23:40:00 0.0 0.0 0.0
...
2015-10-31 23:20:00 0.0 0.0 0.0
2015-10-31 23:30:00 0.0 0.0 0.0
2015-10-31 23:40:00 0.0 0.0 0.0


I need to calculate for each day of the week (monday , tuesday ,.., sunday) the mean of the probability (probab-activ1, probab-activ3 and probab-activ5) durant the 2 last months.

Any idea to solve this problem?

Thank you in advance

Answer

You can use the datetime module and convert your timestamp to a format that is useful for your purpose. For example, you could do:

import datetime

timestamp = '2015-07-31 23:00:00'
day_of_week = datetime.datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S').strftime('%a')

day_of_week
'Fri'
Comments