def MonthsAfter(a): c = a['ShamsiYearMonth'] - a['CohortGroup'] if c>12: return c-88 else : return c# mg['MonthsAfterFirst']mg.apply(lambda x: MonthsAfter(x),axis=1)