我正在尝试使用索引号将前一行值与特定值进行匹配。 但我不知道如何实现这一点。 您能帮我解决这个问题吗?
示例:
operator_type Operator
= None
= AND
= None
= OR
= None
= None
我正在尝试的代码:
for index, row in df.iterrows():
if row['Operator'] == None and row['operator_type'] == '=' and row['Operator'].(index-1).values != 'AND':
print('alka')
错误:
File "imed_cons_new.py", line 68
if row['and_or_not_oprtor'] == None and row['operator_type'] == '=' and row['and_or_not_oprtor'].(index-1).values != 'AND':
^
SyntaxError: invalid syntax
最佳答案
你应该尝试“shift()”
df['previous'] = df['Operator'].shift()
for index, row in df.iterrows():
if row['Operator'] == None and row['operator_type'] == '=' and row['previous'] != 'AND':
print(index,'alka')
输出:
0 alka
4 alka
5 alka
关于python - 如何使用 df.iterrows() 匹配同一列中的前一行值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60389485/