python - Pandas 将多行作为一行,添加特定列

标签 python pandas dataframe loops aggregation-framework

import pandas as pd 

training_data = pd.DataFrame()

training_data['a'] = [401,401.2,410,420,425,426, 426.1]
training_data['b'] = [1,1,2,2,2,3,3]

training_data['condition'] = [True, False, True, True, True,False, False]
我的训练数据:
a         b      condition
401       1        True
401.2     1        False
410       2        True
420       2        True
425       2        True
426       3        False
426.1     3        False
期望的输出:
a         b      condition
401       2        True         (1+1)

410       2        True
420       2        True
425       8        True        (2+3+3)

已删除所有 False 条件,并添加了带有修改值的“b”列。
我怎样才能得到这个想要的输出?
我知道使用 .cumsum()
training_data.query('condition').groupby('grp').agg()

最佳答案

我们来 cumsum

out = training_data.groupby(training_data['condition'].cumsum()).agg({'a':'first','b':'sum','condition':'first'})
Out[271]: 
               a  b  condition
condition                     
1          401.0  2       True
2          410.0  2       True
3          420.0  2       True
4          425.0  8       True

关于python - Pandas 将多行作为一行,添加特定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65439688/

相关文章:

python - SublimeCodeIntel 自动完成在 Pandas 和 Numpy 上失败

python - 转换错误: Failed to convert value(s) to axis units

python-3.x - 如何使用比较多个数据帧并使用 Pandas 返回匹配项

python - 绘制 datetime.time python/matplotlib 的直方图

python - 如何将 IP 转换为向量值

python - Groupby 多列产品聚合

python - bool 索引中列表理解的性能

python - 将字典合并到数据框 get_dummies

r - 在 R 中查找上一行的最后一个字符

python - 使用 scipy.weave.inline 进行快速二维中值过滤