[英]how to increment a value every time a certain condition is met in pandas dataframe
I have a df that looks like this:我有一个看起来像这样的df:
time val
0 1
1 1
2 2
3 3
4 1
5 2
How do I create new columns that hold the cumulative sum of occurance of a condition?如何创建保存条件出现的累积总和的新列? In this case, I want to create a column for each unique value in
val
that holds the cumulative sum at the given row of occurences.在这种情况下,我想为
val
中的每个唯一值创建一个列,该列保存给定出现行的累积总和。 See below:见下文:
time val sum_1 sum_2 sum_3
0 1 1 0 0
1 1 2 0 0
2 2 2 1 0
3 3 2 1 1
4 1 3 1 1
5 2 3 2 1
df['sum_1'] = (df['val'] == 1).cumsum()
df['sum_2'] = (df['val'] == 2).cumsum()
df['sum_3'] = (df['val'] == 3).cumsum()
gives给
time val sum_1 sum_2 sum_3
0 0 1 1 0 0
1 1 1 2 0 0
2 2 2 2 1 0
3 3 3 2 1 1
4 4 1 3 1 1
5 5 2 3 2 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.