Removing zeros from row column in pandas dataframe

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 remove all zeros:

a) that are in front of other numbers and letters (for example, in case 00000000000010SG

I want to remove this part 000000000000

and keep 10SG

).

b) In cases like 20000000000992LG

, I want to remove this part 0000000000

and combine 2

with 992LG

.

str.stprip ('0') only solves the a) part as I tested.

But what is the correct solution for both cases?

+3


source to share


2 answers


I would recommend something similar to Ed's answer, but using a regex to make sure not all 0s are replaced and eliminate the need to hard-code the number 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 line of 0s is replaced.

Thanks to @jezrael for pointing out a small bug in my answer.

+3


source


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 the fixed number with a 0

space, it is not dynamic but for a given dataset this is the easiest task if you can't explain the pattern better

+2


source







All Articles