Suppose I have the following simple dataframe:
df_data=pd.DataFrame({'name':['ABC','ABC XYZ']})
To get the last element I apply:
df_end= pd.DataFrame(df_data.name.str.split().str.get(-1), columns=['name'])
The result is ABC. I'd like to get None when the length of name is less than 2. I have tried the following, but I am not getting right:
df_end['name'] = df_data.name.str.split().apply(lambda x: x[-1] if len(x)>1)
I should not get ABC as the last element for ABC, but I should get XYZ in ABC XYZ