简体   繁体   中英

How to sum subset of columns when meet certain number(condition)

I have a dataframe which is shown in the picture 在此处输入图片说明

I want to add the value of different subsets of non-zero 'ttr' and get the value of 'hour', respectively.

The results should be looked like: hour:7, ttr:15seconds, hour:7, ttr:25seconds, hour:8, ttr:15seconds, and etc.

The difficult part is how to add different subsets of 'ttr' column when meet zero seconds.

Does anybody know how to solve this problem? Thank you!

IIUC filter your df , then using the index to get the continue hour into one group

s=df[df.hour!=0]
s=s.reset_index()
s.groupby([s.hour,s['index'].diff().ne(1).cumsum()]).ttr.sum()
Out[389]: 
hour  index
7     1       00:00:15
      2       00:00:25
Name: ttr, dtype: timedelta64[ns]

Data input

df=pd.DataFrame({'hour':[0,0,7,0,7,7,7],'ttr':[0,0,'00:00:15',0,'00:00:05','00:00:16','00:00:04']})
df.ttr=pd.to_timedelta(df.ttr)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM