Dfs Algorithm In Python Using Pandas Apply Method