Apply function to rows of Dataframe relying on previous function results