I try to reduce the length of a column to 50 and I use this lambda fct:
df['col_1'] = df['col_1'].apply(lambda x: x[:50])
It is working just fine excepting the fact that its cutting the last word, I need a solution that will remove that last word even if the length will decrease with a few characters.
Thank you for any advice on this
Truncate to up to length 50
characters and cut off the last word:
df['col_1'] = df['col_1'].apply(lambda x: ' '.join(x[:50].split(' ')[:-1]) if len(x) > 50 else x)
Note that going the other way around (first cutting off and only then truncating) may and will result in half-words at the end of the sentence.
How does the lambda expression work?
x
, a current sentence to work on