[英]Increment values of a column based on the current row of another column and the previous row of the same column
I want to create a column with multiple conditions wherein if the column ' Batt' contains 'Discharge' and the previous row contains 'none' then increment the value by 1 starting with 0 if not then return the same value without any increments.我想创建一个具有多个条件的列,其中如果列“Batt”包含“Discharge”并且前一行包含“none”,则将该值从 0 开始递增 1,如果不是,则返回相同的值而无需任何递增。
Batt Disch Continous Cycle count
None 0
None 0
Discharge 1
Discharge 1
None 1
Discharge 2
Discharge 2
IIUC, You can create a boolean mask
and then use Series.cumsum
to get the result: IIUC,您可以创建一个boolean mask
,然后使用Series.cumsum
得到结果:
df['Continous Cycle count'] = (
(df['Batt Disch'].eq('Discharge') &
df['Batt Disch'].shift().eq('None'))
.cumsum()
)
Result:结果:
Batt Disch Continous Cycle count
0 None 0
1 None 0
2 Discharge 1
3 Discharge 1
4 None 1
5 Discharge 2
6 Discharge 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.