简体   繁体   English

如何在 python 的 for 循环期间计算值变化?

[英]How to count value change during a for loop in python?

I have a dataframe that consists of one column that consists of 0 and 1 .我有一个 dataframe 由一列组成,该列由01组成。

They are structured in this way [0,0,1,1,0,0,1,1,1,] .它们以这种方式构造[0,0,1,1,0,0,1,1,1,]

My goal is to count only the first 1 in each repeating 1 s in a loop.我的目标是只计算循环中每个重复1秒中的第一个1

So in this example of [0,0,1,1,0,0,1,1,1,] it should be able to only count a total of 2 .所以在这个[0,0,1,1,0,0,1,1,1,]的例子中,它应该只能计算总数2 How can I use a for loop and use an if condition and count this?如何使用 for 循环并使用if条件并计算它?

(As @Erfan mentiond in the comments :) (正如@Erfan 在评论中提到的那样:)

>>> df
   col
0    0
1    0
2    1
3    1
4    0
5    0
6    1
7    1
8    1

>>> df['col'].diff().eq(1).sum()
2

Found a messy way to do it where I can create a translated list and count the sum.找到了一种混乱的方法,我可以创建一个翻译列表并计算总和。

def FirstValue(data):      
for index, item in enumerate(data):    
    if item == 1:
        if data[index-1] == 1:
            counter.append(0)            
    if item == 1:
        if data[index-1] == 0:
            counter.append(1)            
    else:
        counter.append(0)

A simple for loop:一个简单的for循环:

out = [0]+[int(j-i==1) for i,j in zip(lst,lst[1:])]

Output: Output:

[0, 0, 1, 0, 0, 0, 1, 0, 0]

Also, you can assign a pd.Series to a DataFrame column like:此外,您可以将pd.Series分配给 DataFrame 列,例如:

df.col = (pd.Series(lst).diff()==1).astype(int)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM