python - Pandas 按某些 cumsum 拆分行

标签 python pandas dataframe split bins

有没有一种方法可以拆分某些数据帧行,以便我可以使用某些 cumsum 制作一组行?在此示例中,我想拆分使 cumsum 为 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

最佳答案

您可以通过创建一个包含要添加的值 (20-40-60-80...) 和 pd.concat 的数据框来实现用原来的df。然后 drop_duplicates 在 cumsum 列上,以防您在原始数据框中已经有了值 20-40-60...(感谢@jezrael 评论),sort_values 此列和 reset_index。我知道你想 bfill时间戳列并使用 diff在列 cumsum 上重新计算列数。

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

关于python - Pandas 按某些 cumsum 拆分行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62656364/

相关文章:

python - Alembic 的 server_default 和 Postgres

python - py2exe 访问 'other_resources'

python - 按一列对数据进行分组,并从其他两列中选择第一次出现的情况

python - 整个 Pandas DF 的最小 n 值的索引

python - 用 Pandas 替换另一个数据框中数据框中的值

python - 如何将字符串中的单个字母变成大写

python - 如何打印张量的形状值?

python - 如何传递多个输入值并将结果附加回 pandas 数据框

python - pandas 中的逆向查找 : get ordered lists of row- and column-names

r - 计算 R 中数据框列中的不同值