1

I need help in finding the sequence pattern in the column, example

colour --- match
blue    no match
orange  no match
orange  no match
blue    no match
orange  no match
orange  no match
orange  **match**

In the above dataframe, we have 2 columns colour and match. I need to find the sequence in the Column "Color" it should find last 3 value(blue, orange, orange , orange(current cell)) in the column and if it match then need to update the next column match. I am looking this code to work in python code.

jaco0646
  • 15,303
  • 7
  • 59
  • 83
raju kiranlr
  • 15
  • 1
  • 4
  • Might be helpful. [Comparing previous row values in Pandas DataFrame](https://stackoverflow.com/questions/41399538/comparing-previous-row-values-in-pandas-dataframe) – รยקคгรђשค Oct 17 '20 at 12:21

1 Answers1

1

Use shift for that purpose: df["new_col"] = df.col.eq(df.col.shift(1)) & df.col.eq(df.col.shift(2)) to create a boolean Series of values when one in col equals the 2 preceding ones !

output:

0    False
1    False
2    False
3    False
4    False
5    False
6     True
Name: test, dtype: bool

Edit: if you want to convert back to "mismatched"/"matched" values you can simply add this

df["col"] = np.where(df["col"] == False, "mismatched", "matched")

Luc Bertin
  • 326
  • 1
  • 12
  • but i want the cod to find in this format exactly ( current cell (orange) previous cell should be "orange" +"orange" + "blue" then it should type " matched" else not matched – raju kiranlr Oct 17 '20 at 13:16
  • it is giving error s is not defined, and also could you please help in getting in same pattern, ( current cell (orange) previous cell should be "orange" +"orange" + "blue" ) – raju kiranlr Oct 17 '20 at 13:45
  • it worked, real saved my time. Thanks for your valuable inputs Luc Bertin – raju kiranlr Oct 17 '20 at 17:37
  • if there is new data available/written in the excel file, how to make the python to run continues and check for update in excel file and run the above code ? – raju kiranlr Oct 17 '20 at 19:19