python – How to delete database rows according to special conditions and orders?

I’m using the Ubuntu distribution for the GridDB Python Client, I have a dataset that has been converted to a database and is stored in the container. The database has three columns, the columns are keyword, noOfOccurences and class.

I need to delete a specific row which has the noOfOccurences less than 5 and is immediately preceded by a def keyword.

For example, here are five rows and five keywords from the rows are assert, if, def, for and break. The database looks somewhat like this:

keyword noOfOccurences Class
assert 6 1
if 10 0
def 9 3
for 4 2
break 7 1

The code should delete the row containing for since the noOfOccurences is less than 5 and it’s immediately preceded by a def keyword (else, do not delete). The output should be:

keyword noOfOccurences Class
assert 6 1
if 10 0
def 9 3
break 7 1

Is there a way I could do that using a function that takes in the result of an SQL query selecting the rows and columns from the database? Here is the code for the database connection and SQL query:

#...
import griddb_python as griddb
import pandas as pd

factory = griddb.StoreFactory.get_instance()

# Initialize container
try:
  gridstore = factory.get_store(host="127.0.0.1", port="8080",
  cluster_name="hp", username="root",
  password="")
  conInfo = griddb.ContainerInfo("CodeAnalysis ",
    (("keyword", griddb.Type.STRING),
    ("noOfOccurences",griddb.Type.INTEGER),
    ("class", griddb.Type.STRING)
  ),

  griddb.ContainerType.COLLECTION, True)
  cont = gridstore.put_container(conInfo)
  data = pd.read_csv("c_analysis.csv")
  #Add data

for i in range(len(data)):
  ret = cont.put(data.iloc(i, :))
  print("Data has been added successfully")
except griddb.GSException as e:
  print(e)

sql_statement = ('SELECT * FROM CodeAnalysis')
sql_res = pd.read_sql_query(sql_statement, cont)
def function(sql_res):
#...
return newResult