Бин столбец по временным интервалам и рассчитать сумму

MySQL: In general, make necessary changes par your requirement:

UPDATE
    shopping_cart sc
    LEFT JOIN
    package pc ON sc. package_id = pc.id    
SET
    sc. amount = pc.amount
1
задан coldspeed 17 January 2019 в 19:16
поделиться

2 ответа

Используйте reset_index, затем groupby и resample:

df.reset_index(level=0).groupby('computer').resample('4s')['value']\
   .sum().to_frame().sort_index(level=1)

ИЛИ

df.reset_index('computer').groupby('computer').resample('4s')['value']\
   .sum().to_frame().sort_index(level=1)

Выход:

                              value
computer time                      
0        1970-01-01 00:00:00      5
1        1970-01-01 00:00:00      1
2        1970-01-01 00:00:00      5
3        1970-01-01 00:00:00      6
2        1970-01-01 00:00:04      3
3        1970-01-01 00:00:04      4
0
ответ дан Scott Boston 17 January 2019 в 19:16
поделиться

Используйте groupby с pd.Grouper:

u = (df.groupby(['computer', pd.Grouper(key='time', freq='4s')])
       .sum()
       .sort_index(level=1)
       .reset_index())
u['time'] = u['time'].dt.second

u
   computer  time  value
0         0     0      5
1         1     0      1
2         2     0      5
3         3     0      6
4         2     4      3
5         3     4      4

Вместо того, чтобы иметь отдельные groupby и resample, вы можете разрешить одному вызову groupby обрабатывать обе группировки на «компьютере». ", и пересчет" время "вместе.

0
ответ дан coldspeed 17 January 2019 в 19:16
поделиться
Другие вопросы по тегам:

Похожие вопросы: