last day (14 days later) » 

2:51 PM
2
A: Python GroupBy time interval

jezraelI think you need (assuming ts_A is set to DatetimeIndex) GroupBy.mean and omit transform function: #if not datetimeindex #df['ts_A'] = pd.to_datetime(df['ts_A']) #df = df.set_index('ts_A') df = df_a['value'].groupby([df_a['id_A'], df_a['course'], ...

 
Thank you very much for the answer. However, how can I give a name to the new created column that contains the avg?
 
I am working on sample, but it is not easy use your data because pictures.
 
I see. Many Thanks in advance for it.
 
If my answer was helpful, don't forget accept it - click on the check mark () beside the answer to toggle it from greyed out to filled in. Thanks.
 
I used this one but it did not solve the problem either: df = df_a['value'].groupby([df_a['id_A'], df_a['course'], df_a['weight'], pd.TimeGrouper(freq='30S')]).transform(np.mean)
 
2:51 PM
Yes? Some problem?
 
the problem is not solved yet. I need to get what showed above as the expected result.
 
Hmm, maybe you miss I omit transform, only I use mean. Can you check it?
 
I did check it. It did not work.
 
What error? Or bad output?
 
It seems that df.resample should help here, but not sure how to use it with the above code.
bad output : ts_A 2017-04-27 01:35:40 150.000000 2017-04-27 01:35:50 150.000000 2017-04-27 01:36:00 416.666667 2017-04-27 01:36:10 416.666667 2017-04-27 01:36:20 416.666667 2017-04-27 01:36:30 700.000000 2017-04-27 01:36:40 700.000000 2017-04-27 01:36:50 700.000000 2017-04-27 01:37:00 950.000000
 
2:51 PM
Can you check my setup data?
 
what you mean? where is it?
I can send you my csv files if it is more appropriate
 
I add to my answer to end sample data. also resample solution was added, but same output.
 
THANKS YOU JEZRAEL. yes with resample and .reset_index() the problem has been solved.
 
Supeeeeer, I am really happy ;) Please accept solution - click on the check mark (). Thanks.
hi
 
Hi Jezrael
 
2:52 PM
Hi Carlo
 
how can I add an extra column (called time_spent) as the cumulative time differences?
time_spent in seconds should contains: [0, 30, 60, 90, 120,.....]
thank you in advance for your help.
please.
 
is is not so easy
hmmm
 
Yes, I am trying for quite long time.
 
Yes, I would like help you, but now I have to go home and in SO I want answer tomorrow. So you have 2 choices:
use my sample and create new question in Stackoverflow:
d = {'weight': {0: 3.5, 1: 3.5, 2: 3.5, 3: 3.5, 4: 3.5, 5: 3.5, 6: 3.5, 7: 3.5, 8: 3.5, 9: 3.5, 10: 5.0, 11: 5.0, 12: 5.0, 13: 5.0, 14: 5.0, 15: 5.0, 16: 5.0, 17: 5.0, 18: 5.0, 19: 5.0}, 'value': {0: 100, 1: 200, 2: 350, 3: 400, 4: 500, 5: 600, 6: 700, 7: 800, 8: 900, 9: 1000, 10: 100, 11: 200, 12: 450, 13: 300, 14: 600, 15: 500, 16: 522, 17: 540, 18: 320, 19: 900}, 'ts_A': {0: '2017-04-27 01:35:40', 1: '2017-04-27 01:35:50', 2: '2017-04-27 01:36:00', 3: '2017-04-27 01:36:10', 4: '2017-04-27 01:36:20', 5: '2017-04-27 01:36:30', 6: '2017-04-27 01:36:40', 7: '2017-04-27 01:36:50', 8: '2017-04
 
Thanks. yes this is what I am going to do
 
3:03 PM
Or if you want really from me to help you, send me your sample to my email with desired output and tomorrow I help you. But faster is first option.
 
OK. what is your email? just in case
I will open a new question
 
my email is in my profile ;)
 
perfect
I just sow it
many Thansk
Thanks.
 
glad can help, good luck
bye
 
thanks. bye
 

last day (14 days later) »