我正在处理Pandas数据框,并遍历各行以查找相关数据,在这种情况下为颜色。在大多数情况下,“颜色”列的单元格将被填写,在这种情况下,我不需要在该行中的其他单元格中搜索颜色,可以继续到下一行。但是,如果不存在“颜色”列,则Pandas将引发KeyError
,我将需要创建该列并为其添加值。这是我的代码段:
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
当我尝试执行该代码块时,我得到了SyntaxError
。我已经阅读了e-satis的comment,但是,如果我正确理解了yield
,这将终止函数,而不是循环的此迭代。我试图用Python进行的操作甚至有可能吗?
我没有太多使用对象和类的练习,所以如果您也想在这里给我反馈,请