I have a dataframe that consists of one column that consists of 0
and 1
.
They are structured in this way [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.
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
. How can I use a for loop and use an if
condition and count this?
(As @Erfan mentiond in the comments :)
>>> 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:
out = [0]+[int(j-i==1) for i,j in zip(lst,lst[1:])]
Output:
[0, 0, 1, 0, 0, 0, 1, 0, 0]
Also, you can assign a pd.Series
to a DataFrame column like:
df.col = (pd.Series(lst).diff()==1).astype(int)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.