I am trying to run a regression of a list of bond's values against the credit (S&P) rating of the bond. For that I am trying to create a binary matrix of where the columns (a list of all available S&P credit rating [AAA, AA+,..., BBB-, etc..]). My code takes several hours to run, I was wondering if there was a faster way to create a binary matrix to later run a regression, than the code below.
ratg = ['AAA', 'AA+', 'AA', 'AA-', 'A+', 'A', 'A-', 'BBB+', 'BBB', 'BBB-', 'BB+', 'BB', 'BB-', 'B+', 'B', 'B-', 'CCC']
sizefile = len(datafile)
binaryrat = []
s = []
for i in range(sizefile):
for k in range(lenrat):
x = datafile['RatingGrp'].iloc[i] == ratg[k]
s.append(x)
binaryrat.append(s)