Reputation: 471
I have the following df:
Invoice number
.5
.3
02
I want to replace the '.' because it gets captured incorrectly. This is succesful with the following code:
df['Invoice number'] = df['Invoice number'].astype(str).str.replace(r"[^a-zA-Z0-9\$?!]", '')
however, a 0 is placed in front of the number resulting in the following df:
Invoice number
05
03
02
I've tried the following
for i, row in df.iterrows():
if str(i).startswith('.'):
df.loc[i, 'KvK'] = df['KvK'].astype(str).str.replace(r"[^a-zA-Z0-9\$]", '')
df.loc[i,'Factuurnummer'] = df['Factuurnummer'].astype(str).str.replace(r"[^a-zA-Z0-9\$]", '')
However, this does not work. Nothing happens.
I would like the following output:
invoice #
5
3
02
Upvotes: 2
Views: 131
Reputation: 75080
you can do this with a mask created by series.str.startswith
and series.mask
or np.where
:
s = df['Invoice number']
s.mask(s.str.startswith('.'),s.str.replace(".",""))
Or if one is comfortable using a regex
pattern, one can also use:
df['Invoice number'].str.replace(r'^\.', '')
0 5
1 3
2 02
Name: Invoice number, dtype: object
Upvotes: 2