我有数据集。对于特定条件,有一列具有True或False值。如果有一系列行具有相同的值,则让这些行的计数器相同。但是我还需要考虑行中的步骤,如果其大于1,则开始新的标志值 为了清楚起见,下面是我的代码:
import pandas as pd
from pandas import Series, ExcelWriter
import numpy as np
import datetime as dt
file=pd.ExcelFile("6324_data.xlsx")
Dataframe=file.parse('sheet1')
c1 = Dataframe['change']
row_iterator = Dataframe.iterrows()
c1 = Dataframe['change']
res = []
var = 1
cur=c1[0]
d1=Dataframe['index_col']
flag = 0
Av=d1[0]
res.append(None)
row_iterator = Dataframe.iterrows()
idx, last = row_iterator.__next__()
for index, row in row_iterator:
for val in c1[1:]:
if val == cur and flag == 0:
res.append(var)
elif val == cur and flag == 1:
var+=1
flag = 0
res.append(var)
elif val != cur and flag == 0:
flag = 1
res.append(var)
elif val != cur and flag == 1:
res.append(var)
else:
pass
例如,当前代码带来此输出
x2=[
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13]
预期输出为
x2=[
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
13,
14,
14,
14,
14,
14,
14,
14,
14,
14,
14,
14,
14,
14,
14,
14,
15,
15,
15,
15,
15,
15,
15,
15,
15]
行索引的变化如下
row_index = [
19327,
19328,
19329,
19330,
19331,
19332,
19333,
19334,
19335,
19336,
19337,
19338,
19339,
19340,
19341,
19342,
19343,
19344,
19345,
19346,
19347,
19348,
19349,
19350,
19351,
19352,
19377,
19378,
19379,
19380,
19381,
19382,
19383,
19384,
19385,
19386,
19387,
19388,
19389,
19390,
19391,
19402,
19403,
19404,
19405,
19406,
19407,
19408,
19409,
19410
]
在行索引等于19352和19391之后,步长大于1。
仅在行索引或(Av)等于1的情况下,我才能如何使for循环依赖于