Compare dataframe previous and current row in Python

Question:

I have a dataframe with the following column.

CUI
C13874
C13874
C47687

I have written a function that calls a certain API endpoint. I would like to only call the API endpoint when the value of ‘CUI’ column changes, else write the previous output.

umls_cui = open('umls_cui_names.txt', 'w')

def get_cui(CUI):
        #if CUI(prev) !=  CUI(current):
           #api key
           API = "ssssssssssssss"
           #set the url
           url = 'https://uts-ws.nlm.nih.gov/rest/content/current/CUI/'
           url_cui = url + CUI
           #set the header
           headers = {'Content-Type': 'application/json'}
           #set the parameters
           params = {'apiKey' : API}

           #send the request
           response = requests.get(url_cui, headers = headers, params = params)
          name = response.json()['result']['name']
          umls_cui.write("%st%sn" % (CUI, name))
      #else:
        #umls_cui.write("%st%sn" % (CUI, name))


for i in df['CUI']:
    get_cui(i)

umls_cui.close()
umls_cui_names.txt.close()
Asked By: rshar

||

Answers:

You can try assigning the used value to a variable and check if that value is repeating in each iteration (I made a dummy function to show how it works):

import pandas as pd

def get_cui(CUI):
    print('calling:', CUI)
    return CUI

data = ['C13874', 'C13874', 'C47687']
df = pd.DataFrame(data, columns=['CUI'])
print(df)

v = ''
for i in df['CUI']:
    if i != v:
        v = get_cui(i)
    else:
        print('Skipping:', i)

This is the output:

calling: C13874
Skipping: C13874
calling: C47687
Answered By: DeusDev
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.