Say I have a list like this:
Words
One
Twos
Threes
Four
Fives
...
Thousand
Is there a relatively simple way to remove the 'S' from the end of the words? So it would become:
Words
One
Two
Three
Four
Five
...
Thousand
Say I have a list like this:
Words
One
Twos
Threes
Four
Fives
...
Thousand
Is there a relatively simple way to remove the 'S' from the end of the words? So it would become:
Words
One
Two
Three
Four
Five
...
Thousand
You can use np.where
to test if the last character is an 's' and if so remove it, otherwise use the existing value, this will be vectorised and much faster than a list comprehension:
In [14]:
df['Words'] = np.where(df['Words'].str[-1] == 's', df['Words'].str[:-1], df['Words'])
df
Out[14]:
Words
0 One
1 Two
2 Three
3 Four
4 Five
5 Thousand
An alternative method which is likely to be even faster is to use loc
and your condition to only strip the last character where it contains a trailing s, all other rows are left untouched:
In [18]:
df.loc[df['Words'].str[-1] == 's','Words'] = df['Words'].str[:-1]
df
Out[18]:
Words
0 One
1 Two
2 Three
3 Four
4 Five
5 Thousand