Replace all but last occurrences of a character in a string with pandas
Regex-based with str.replace
This regex pattern with str.replace
should do nicely.
s.str.replace(r'\.(?=.*?\.)', '')
0 1234.5
1 123.5
2 2345.6
3 678.9
dtype: object
The idea is that, as long as there are more characters to replace, keep replacing. Here's a breakdown of the regular expression used.
\. # '.'
(?= # positive lookahead
.*? # match anything
\. # look for '.'
)
Fun with np.vectorize
If you want to do this using count
, it isn't impossible, but it is a challenge. You can make this easier with np.vectorize
. First, define a function,
def foo(r, c):
return r.replace('.', '', c)
Vectorize it,
v = np.vectorize(foo)
Now, call the function v
, passing s
and the counts to replace.
pd.Series(v(s, s.str.count(r'\.') - 1))
0 1234.5
1 123.5
2 2345.6
3 678.9
dtype: object
Keep in mind that this is basically a glorified loop.
Loopy/List Comprehension
The python equivalent of vectorize
would be,
r = []
for x, y in zip(s, s.str.count(r'\.') - 1):
r.append(x.replace('.', '', y))
pd.Series(r)
0 1234.5
1 123.5
2 2345.6
3 678.9
dtype: object
Or, using a list comprehension:
pd.Series([x.replace('.', '', y) for x, y in zip(s, s.str.count(r'\.') - 1)])
0 1234.5
1 123.5
2 2345.6
3 678.9
dtype: object