I want to get the first half of a string from a pandas dataframe column, where the length varies row by row. I have searched around and found questions like this but the solutions all focus on delimeters and regular expressions. I don't have a delimiter - I just want the first half of the string, however long it is.
I can get as far as specifying the string length I want:
import pandas as pd
eggs = pd.DataFrame({"id": [0, 1, 2, 3],
"text": ["eggs and spam", "green eggs and spam", "eggs and spam2", "green eggs"]})
eggs["half_length"] = eggs.text.str.len() // 2
and then I want to do something like eggs["truncated_text"] = eggs["text"].str[:eggs.half_length]
. Or is defining this column the wrong way to go in the first place? Can anyone help?