I am trying to use pandas to develop some trend data for a dataset I am working on. The set consists of a product number, fiscal year, and number of units sold. I want to create a fourth 'trend' column that will compare each product to itself by fiscal year, specifically the year before it. The first trend for each product should always be 0 since there is no data for the previous year. Trend is simply a "+" if the value has gone up since the previous year for that product or a "-" if the opposite is true. I thought this was pretty straightforward but apparently I'm overlooking something.
data_out.sort_values(['product_number','fiscal_year'], inplace = True)
trend = []
for n, units in enumerate(data_out.Units_Sold):
if n == 0:
trend.append(0)
elif data_out.Units_Sold[i-1]>=units and data_out.product_number.loc[i] == data_out.product_number.loc[i-1]:
trend.append(0)
elif data_out.Num_Contrs[i-1]<units and data_out.product_number.loc[i] == data_out.product_number.loc[i-1]:
trend.append(0.08)
data_out.Trend = trend
Edit:
