是否有一种方法可以拆分某些数据帧行,以便我可以用一定的累积量来创建一组行?在此示例中,我要拆分累积20的行
my data
timestamp counts cumsum
'2015-01-01 03:45:14' 4 4
'2015-01-01 03:45:14' 2 6
'2015-01-01 03:45:14' 1 7
'2015-01-01 03:45:15' 12 19
'2015-01-01 03:45:15' 8 27 <--split
'2015-01-01 03:45:15' 8 35
'2015-01-01 03:45:15' 2 37
'2015-01-01 03:45:16' 26 63 <--split(twice)
'2015-01-01 03:45:17' 3 66
'2015-01-01 03:45:17' 8 71
'2015-01-01 03:45:19' 11 82 <--split
'2015-01-01 03:45:20' 8 90
'2015-01-01 03:45:21' 1 91
我希望我的数据框像这样
我的数据
timestamp counts cumsum
'2015-01-01 03:45:14' 4 4
'2015-01-01 03:45:14' 2 6
'2015-01-01 03:45:14' 1 7
'2015-01-01 03:45:15' 12 19
'2015-01-01 03:45:15' 1 20 <--split 20
'2015-01-01 03:45:15' 7 27 <--split
'2015-01-01 03:45:15' 8 35
'2015-01-01 03:45:15' 2 37
'2015-01-01 03:45:16' 3 40 <--split 40
'2015-01-01 03:45:16' 20 60 <--split 60
'2015-01-01 03:45:16' 3 63 <--split
'2015-01-01 03:45:17' 3 66
'2015-01-01 03:45:17' 8 71
'2015-01-01 03:45:19' 9 80 <--split 80
'2015-01-01 03:45:19' 2 82 <--split
'2015-01-01 03:45:20' 8 90
'2015-01-01 03:45:21' 1 91
答案 0 :(得分:5)
您可以通过创建要添加值(20-40-60-80 ...)并使用原始df pd.concat
的数据框来做到这一点。然后,如果您在原始数据帧中已经有了20-40-60 ...的值(感谢@jezrael注释),请在“累积量”列上drop_duplicates
,sort_values
此列和reset_index
。我们了解到您想bfill
时间戳列,并在列总和上使用diff
重新计算列数。
val_split = 20
df_ = (pd.concat([df,
pd.DataFrame({'cumsum':range(val_split, df['cumsum'].max(), val_split)})])
.drop_duplicates('cumsum')
.sort_values('cumsum')
.reset_index(drop=True)
)
df_['timestamp'] = df_['timestamp'].bfill()
df_['counts'] = df_['cumsum'].diff().fillna(df_['counts'])
print (df_)
timestamp counts cumsum
0 '2015-01-01 03:45:14' 4.0 4
1 '2015-01-01 03:45:14' 2.0 6
2 '2015-01-01 03:45:14' 1.0 7
3 '2015-01-01 03:45:15' 12.0 19
4 '2015-01-01 03:45:15' 1.0 20
5 '2015-01-01 03:45:15' 7.0 27
6 '2015-01-01 03:45:15' 8.0 35
7 '2015-01-01 03:45:15' 2.0 37
8 '2015-01-01 03:45:16' 3.0 40
9 '2015-01-01 03:45:16' 20.0 60
10 '2015-01-01 03:45:16' 3.0 63
11 '2015-01-01 03:45:17' 3.0 66
12 '2015-01-01 03:45:17' 5.0 71
13 '2015-01-01 03:45:19' 9.0 80
14 '2015-01-01 03:45:19' 2.0 82
15 '2015-01-01 03:45:20' 8.0 90
16 '2015-01-01 03:45:21' 1.0 91