I am having a nan value when trying to get left part of a string a pandas dataframe, where the left condition is depending on the lengh of the cell in another column of the dataframe :
Example of df :
Phrase | Color |
---|---|
Paul like red | red |
Mike like green | green |
John like blue | blue |
My objectives is to obtain a series of the first part of the phrase => before "like {Color}". Here it would be :
|First Name|
| Paul | | Mike | | John |
i try to call the function below :
df["First Name"] = df["Phrase"].str[:- df["Color"].str.len() - 6]
But i keep having Nan value results. It seems my length calculation of the colors can't transmit to my str[:-x]
function.
Can someone help me understand what is happening here and find a solution ?
Thanks a lot. Have a nice day.