I am working with a Pandas dataframe and iterating over the rows to find relevant data, in this case, colors. For the most part, the 'Color' column's cell will be filled out, in which case I do not need to search other cells in the row for colors and can continue to the next row. However, if no 'Color' column is present, Pandas will raise a KeyError
and I will need to create the column and add values to it. Here is a snippet of my code:
class Reader(object):
def __init__(self, dataframe):
"""Grabs column headers, converts dataframe from unicode string objects to
Python string objects, sets dataValues class variable equal to a numpy array of the sheet"""
self.headers = dataframe.columns.values
self.dataFrame = dataframe.applymap(str)
self.dataValues = self.dataFrame[:].values
class Extractor(object):
"""Extracts relevant spec information from a sku (row)"""
def getColor(self, colorRef):
"""Looks in Model Name and Description for color information.
Uses pre-existing color database (colorRef).
Returns dataFrame column 'Color' """
index = 0
newColorArray = [] #this will not be returned, its sole purpose is to keep track of colors
for sku in myData.dataValues:
try:
if myData.dataFrame.iloc[index, myData.dataFrame.columns.get_loc('Color') != ('' or ' '):
continue #color exists, move to next row
except KeyError:
newColorArray = ['']*myData.dataFrame.shape[0]
#Remaining code searches for colors
When I try to execute that block of code, I get a SyntaxError
. I have read e-satis's comment but, if I understand yield
correctly, this will terminate the function and not this iteration of the loop. Is what I am trying to do even possible in Python?
I do not have a lot of practice using objects and classes, so if you'd like to give me feedback there as well, please do.