I am still a beginner in python. I am trying to find a common value with if statement,
import pandas as pd
df = pd.read_csv("data.csv")
for n in range(2, len(df)):
if df.loc[n].isin([2]).any():
if df.loc[n-1].isin([1]).any():
print("Yes")
n += 1
| A | B |
|---|---|
| 1 | 3 |
| 2 | 3 |
| 1 | 8 |
| 1 | 2 |
| 3 | 5 |
| 2 | 7 |
I want it to be printed "Yes" if every time 2 is in n and 1 is in n-1 until the end of data. not just if it matches once. If ever 2 is in n and n-1 doesn't have any value 1, then it won't be printed. For example the above table won't be printed as not all 2 in n doesn't have 1 in n-1
n += 1at the end? If that's there by accident, please edit to remove it.pd.read_csv("data.csv")since that's not relevant to the question; instead it would be better to provide the data in the code. See How to make good reproducible pandas examples. (See also minimal reproducible example.)n-1? In that case, I can show you.print("No")for non-matching rows. Speaking of that, there's no need to nestifs here when you could useandinstead. That would save you one level of nesting.