- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题是 moys 问题的延伸,因为我对如何根据 bool 值的真值序列进行求和的答案感兴趣。假设我有这个数据框,我只想对真实行进行求和。:
id log loc pos_evnts neg_evnts As non_As pos_wrds neg_wrds As/Ac Truth T
0 A c City 8 0 48 0 0 0 1 False 1
1 A d City 2 6 0 180 4 10 0 True 2
2 A e City 0 22 87 0 0 0 1 True 2
3 A f City 8 0 35 0 0 0 1 False 3
4 A g City 8 2 42 0 0 0 1 False 3
5 A h City 4 4 0 115 4 2 0 True 4
6 A i City 2 0 32 0 0 0 1 True 4
7 B j Hill 3 0 24 0 0 0 1 False 5
8 B k City 6 8 116 0 0 2 1 False 5
9 B l City 2 4 200 0 0 2 1 False 5
10 C m City 2 0 40 0 0 0 0 True 6
11 C n Hill 5 0 1 0 2 0 0 True 6
12 C o City 5 0 7 0 0 5 1 True 6
我想对行进行求和以获得这个答案(真实的行是求和的):
pos_evnts neg_evnts As non_As pos_wrds neg_wrds As/Ac
0 8 0 48 0 0 0 1
1 2 6 0 180 4 10 0
2 2 28 87 180 4 10 1
3 8 0 35 0 0 0 1
4 8 2 42 0 0 0 1
5 4 4 0 115 4 2 0
6 6 4 32 115 4 2 1
7 3 0 24 0 0 0 1
8 6 8 116 0 0 2 1
9 2 4 200 0 0 2 1
10 2 0 40 0 0 0 0
11 7 0 41 0 2 0 0
12 12 0 48 0 2 5 1
我尝试过:
df.groupby((df['T'])).cumsum()
In [4738]: df.groupby(df['T']).cumsum()
Out[4738]:
pos_evnts neg_evnts As non_As pos_wrds neg_wrds As/Ac Truth
0 8 0 48 0 0 0 1 0.000
1 2 6 0 180 4 10 0 1.000
2 2 28 87 180 4 10 1 2.000
3 8 0 35 0 0 0 1 0.000
4 16 2 77 0 0 0 2 0.000
5 4 4 0 115 4 2 0 1.000
6 6 4 32 115 4 2 1 2.000
7 3 0 24 0 0 0 1 0.000
8 9 8 140 0 0 2 2 0.000
9 11 12 340 0 0 4 3 0.000
10 2 0 40 0 0 0 0 1.000
11 7 0 41 0 2 0 0 2.000
12 12 0 48 0 2 5 1 3.000
但它是错误的(真相:0.000 行)。我希望它只对真实行进行求和。任何帮助,将不胜感激。如何修改公式以忽略 cumsum 的 False 行。
最佳答案
您可以仅过滤包含数字列的 True
行,也排除 T
列以防止累积总和并分配回来:
cols = df.select_dtypes(np.number).columns.difference(['T'])
df.loc[df['Truth'], cols] = df.loc[df['Truth'], cols] .groupby(df['T']).cumsum()
print (df)
id log loc pos_evnts neg_evnts As non_As pos_wrds neg_wrds As/Ac \
0 A c City 8 0 48 0 0 0 1
1 A d City 2 6 0 180 4 10 0
2 A e City 2 28 87 180 4 10 1
3 A f City 8 0 35 0 0 0 1
4 A g City 8 2 42 0 0 0 1
5 A h City 4 4 0 115 4 2 0
6 A i City 6 4 32 115 4 2 1
7 B j Hill 3 0 24 0 0 0 1
8 B k City 6 8 116 0 0 2 1
9 B l City 2 4 200 0 0 2 1
10 C m City 2 0 40 0 0 0 0
11 C n Hill 7 0 41 0 2 0 0
12 C o City 12 0 48 0 2 5 1
Truth T
0 False 1
1 True 2
2 True 2
3 False 3
4 False 3
5 True 4
6 True 4
7 False 5
8 False 5
9 False 5
10 True 6
11 True 6
12 True 6
关于python - 在 Pandas 中,如何仅使用 True 系列 bool 值对行进行求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59370130/
我正在尝试在 OCaml 中创建一个函数,该函数在数学中执行求和函数。 我试过这个: sum n m f = if n = 0 then 0 else if n > m then f
我正在尝试找到一个可以帮助我解决问题的公式。 这个公式应该对每个靠近(总是在左边)具有相同名称的单元格的单元格求和(或工作)。如下所示: 将每个大写字母视为 “食谱”并且每个小写字母为 “成分” .在
让它成为以下 python pandas DataFrame,其中每一行代表一个人在酒店的住宿。 | entry_date | exit_date | days | other_columns
我有显示客户来电的数据。我有客户号码、电话号码(1 个客户可以有多个)、每个语音调用的日期记录以及调用持续时间的列。表看起来如下示例。 CusID | PhoneNum | Date
让它成为以下 python pandas DataFrame,其中每一行代表一个人在酒店的住宿。 | entry_date | exit_date | days | other_columns
我得到了两列数据; 答: 2013年12月31日 2013年12月30日 2013年12月29日 2013年12月28日 2013年12月27日 2012年12月26日 B: 10 10 10 10
我对 double 格式的精度有疑问。 示例: double K=0, L=0, M=0; scanf("%lf %lf %lf", &K, &L, &M); if((K+L) 我的测试输入: K
我有以下数组: int[,] myArray1 = new int[2, 3] { { 1, 2, 3 }, { 4, 6, 8 } }; int[,] myArray2 = new int[2, 3
我需要有关报告查询的帮助。我在该方案的底部有一个发票表,需要一种方法来获取总计费金额,同时在此数据库方案中的较高点进行条件过滤。我需要加入其他表,这会导致 SUM 函数返回不需要的结果。 这是我正在使
我有一个使用innodb作为存储引擎的MySQL数据库,并且我有许多采用基本形式的查询: SELECT bd.billing, SUM(CASE WHEN tc.transaction_class
尝试创建一个查询来给出总胜、平和负。我有以下查询 SELECT CASE WHEN m.home_team = '192' AND m.home_full_time_score
我正在尝试生成一份报告,显示排名靠前的推荐人以及他们推荐的人产生了多少收入。 这是我的表格的缩写版本: Users Table ------------------ id referral_user_
我有以下查询,并得到了预期的结果: SELECT IF (a1>b1,'1','0') AS a1r, IF (a2>b2,'1','0') AS a2r,
我尝试了几种不同的解决方案,但都没有成功。我给出的表格是一个示例,其设计和功能与我实际使用的表格类似: PK | Color | Count -------------------
我正在尝试构建一个查询来检查我的库存。 SELECT COUNT(*) AS item_count, reseller_id, sum(sold) as sold_count, sum(refunde
我试图解决一个看起来像下面编写的代码的问题,但由于缺乏知识和阅读 sqlalchemy 文档,我还没有真正找到解决问题的方法。 目标: 如果 year_column 中的年份相同,则获取 sales_
我有一个包含一周中多天的表格。一周中的每一天都有独特的属性,例如冰淇淋是否在这一天成功送达: ID DAY_WEEK ICE_CREAM 1 Monday
首先,我有一个名为store_00的表 id | ref | item | qty | cost | sell 1 22 x1 5 10 15 2 22
我正在编写一个程序,计算每个数字的总和,直到 1000。例如,1+2+3+4+5....+100。首先,我将求和作业分配给 10 个处理器:处理器 0 得到 1-100,处理器 1 得到 101-20
我想在一个循环中一次对多个属性求和: class Some(object): def __init__(self, acounter, bcounter): self.acou
我是一名优秀的程序员,十分优秀!