I am new to Python and I am trying to use indexing to obtain the last name along each row.
import numpy as np import pandas as pd def manager(vec): for i,val in enumerate(vec): if val == np.NaN: break return vec[i - 1] df = pd.DataFrame({'ID':[23,15,20], 'L1_name': ['Andrew','Thomas','Thomas'], 'L1_ID': [234,994,994],'L2_Name':['Andrew','Alice','Thomas'],'L2_ID':[234,237,994], 'L3_Name':['Jerico','Sarah',np.nan],'L3_ID':[453,237,np.nan]}) df['new'] = df.apply(manager, axis=1) df What am i doing wrongly, anyone?
managerreturns in the first round of the foor loop, either withvec[- 1]orNone.