Pandas check column and add leading zeros

Solution for Pandas check column and add leading zeros
is Given Below:

Hello guys and experts.

I hope somebody can help me to solve this issue.

I have a csv file that is structured as follow.

@timestamp.    message.          name.                user
time.          something.        something.           123456
time.          something         something            1234
time.          something.        something.           hello1

What I want to do, is check the user field, which is a string, and add leading zeros if there are non and if the total amount of numbers is less than six. But this logic should apply only if the user doesn't contain any alphabetic letter.

This is the bit where I am very confused. How can I make a distinction between numbers (as string) and alphanumeric strings and apply the logic

just to make this example clear, this is the output I am looking for.

@timestamp.    message.          name.                user
time.          something.        something.           123456
time.          something         something            001234
time           something         something            hello1

Basically I need first to check if the number of digits is less than 6..and if it is less, to add the diff as leading zero.

looking on some forums and other topics, I come across this solution

df['user'] = df['user'].apply(lambda x: '{0:0>15}'.format(x))

But I don’t understand fully the functionality of the lambda and it is not exactly what I am looking for.

Can please please anyone help me to sort out this issue?

Thank you so much in advance for any help guys.

Following some advice here, I went with this script:

df['user'] = df['user'].map(lambda x: x.lstrip('-').rstrip(' - '))
df["user"] = df["userID"].str.replace(
    r"^d{1,5}$", lambda g: "{:0>6}".format(, regex=True
df.to_csv('./test.csv', index=False)

The print statement it shows that the leading zeros has been added correctly, but when I open the saved file, there are not there, there are no leading zero on the csv file.

You can use .str.replace:

df["user"] = df["user"].str.replace(
    r"^d{1,5}$", lambda g: "{:0>6}".format(, regex=True

This will add leading zeros to cells that contains only 1 to 5 digits:

  @timestamp.    message.       name.    user
0       time.  something.  something.  123456
1       time.   something   something  001234
2       time.  something.  something.  hello1

You can also use zfill

Test dataframe:

import pandas as pd
df = pd.DataFrame({"user": ["123456", "1234", "hello1"]})

Use zfill:



0    123456
1    001234
2    hello1
Name: user, dtype: object


import re
def padNum(x):
    zeros = 6 - len(x)
    if zeros > 0:
        return str('0' * zeros) + x
        return x

df['user'] = [padNum(x) for x in df['user'] if bool('d+', x))]



  @timestamp    message       name    user
0       time  something  something  123456
1       time  something  something  001234
2       time  something  something  hello1