Using pandas, I want to derive a last name column for a set of first names that are 4 or more characters long.
I have tried these:
data = pd.read_csv("Data.csv")
data
#split the EmployeeName into firstname and lastname
flname = data['EmployeeName'].str.split(expand=True)
flname
#add first name column to data frame
data['FirstName'] = flname[0]
#apply condition on first name
dfname = data['FirstName'].apply(lambda x:x if len(x) \> 4 else None)
dfname = dfname.dropna()
#add last name and new first name columns to data frame
data['LastName'] = flname[0]
data['NewFirstName'] = dfname
data
#This is the wrong bit that throws an error
derived_name = data.apply(lambda x:x if data\['FirstName'\] in data\['NewFirstName'\] else None)
derived_name.dropna()
#TypeError: unhashable type: 'Series'
#Are there shorter ways of writing these code lines with pandas?