Reputation: 175
I have a column in my dataframe,where the values are something like this:
col1:
00000000000012VG
00000000000014SG
00000000000014VG
00000000000010SG
20000000000933LG
20000000000951LG
20000000000957LG
20000000000963LG
20000000000909LG
20000000000992LG
I want to delete all zeros:
a)that are in front of other numbers and letters(For example in case of 00000000000010SG
I want to delete this part000000000000
and keep 10SG
).
b) In cases like 20000000000992LG
I want to delete this part 0000000000
and unite 2
with 992LG
.
str.stprip('0') solves only part a), as I checked.
But what is the right solution for both cases?
Upvotes: 3
Views: 1284
Reputation: 403218
I would recommend something similar to Ed's answer, but using regex to ensure that not all 0s are replaced, and the eliminate the need to hardcode the number of 0s.
In [2426]: df.col1.str.replace(r'[0]{2,}', '', 1)
Out[2426]:
0 12VG
1 14SG
2 14VG
3 10SG
4 2933LG
5 2951LG
6 2957LG
7 2963LG
8 2909LG
9 2992LG
Name: col1, dtype: object
Only the first string of 0s is replaced.
Thanks to @jezrael for pointing out a small bug in my answer.
Upvotes: 4
Reputation: 394459
You can just do
In[9]:
df['col1'] = df['col1'].str.replace('000000000000','')
df['col1'] = df['col1'].str.replace('0000000000','')
df
Out[9]:
col1
0 12VG
1 14SG
2 14VG
3 10SG
4 2933LG
5 2951LG
6 2957LG
7 2963LG
8 2909LG
9 2992LG
This will replace a fixed number of 0
s with a blank space, this isn't dynamic but for your given dataset this is the simplest thing to do unless you can explains better the pattern
Upvotes: 2