对于上下文,Tempo有温度和0值,我希望在我的dataframe中创建一个计数器。我在网上看到了一些解决这个问题的方法,但恐怕它们太复杂了,我无法理解/实现。
这是我的密码:
prev_temp, cnt, n= -300, 0, 0
for row in df.iterrows():
if row.Tempo!=0 and prev_temp==0:
cnt+=1
n+=1
prev_temp=rows.Tempo
elif row.Tempo==0 and prev_temp!= 0:
prev_temp=rows.Tempo我所犯的错误是
AttributeError:'tuple‘对象没有属性'Tempo'
发布于 2021-01-17 17:02:53
方法.iterrows()返回元组:(索引,序列) (Series -基本上是一行)
试试这个片段:
prev_temp, cnt, n= -300, 0, 0
for index, row in df.iterrows():
if row.Tempo!=0 and prev_temp==0:
cnt+=1
n+=1
prev_temp=rows.Tempo
elif row.Tempo==0 and prev_temp!= 0:
prev_temp=rows.Tempo因此,在上面的示例中,我将for row in df.iterrows():替换为for index, row in df.iterrows():,以便迭代元组项,而不是元组本身。
顺便说一句,我不明白rows在您的代码中代表什么。
https://stackoverflow.com/questions/65763498
复制相似问题