reduce the time complexity for python for loop

Viewed 2608

I have python data frame with the following columns:

Index([u'Academic Period', u'Academic Period Desc', u'Student ID',
       u'Subject', u'Course Number', u'Course Reference Number',
       u'Course Identification', u'Schedule Type', u'Instructor's ID',
       u'Highest Degree', u'Highest Degree Code',
       u'Instructor Position Employee Group'],
      dtype='object')

I'm now trying to loop by rows, check and compare two column values:

for i in df['Student ID']:
    if df['Course Reference Number'] >= 50000:
        if df['Highest Degree Code'] <= 7:
            print df["Instructor's ID"]
    else:
        if df['Highest Degree Code'] <= 6:
            print df["Instructor's ID"]

I have 910,000+ rows in my file and it's taking too long to loop through.

Can i reduce the time complexity from O(n) to lower(with any possible algorithm)?

Thanks!

2 Answers
Related