Add leading zeros in Python pandas (preceding zeros in data frame)

add Leading Zeros or preceding zeros to column of a data frame in python pandas is depicted  with an example. we will be filling the preceding zeros to integer column and  string column to the desired length using zfill() function. zfill() Function in Python pads string on the left with zeros to the required width.

Lets see an example of each of the following

  • add leading zeros to the character column in pandas python
  • add preceding zeros to the numeric column in pandas python
  • add leading zeros the string in python using zfill() Method
  • add preceding and trailing zeros after decimal using rjust() and ljust() Method in python
  • add preceding zeros of the character and numeric column using rjust() and ljust() function

Objective: 

add leading zeros in python

Lets look how to do it with an example.

 

ADD leading Zeros in python to the Numeric column:

Create a simple DataFrame:

# create dataframe

import pandas as pd
d = {'Col1' : [1,200,3000,40000]}
df=pd.DataFrame(d)
df

Which results in a dataframe as shown below.

Add leading zeros in Python pandas (preceding zeros in data frame) 1

Add the leading zeros to numeric column in Python pandas

## Add leading zeros to the integer column in Python

df['Col2']=df['Col1'].apply(lambda x: '{0:0>10}'.format(x)) 
df

Col1 is replaced with leading zeros till the length of the field reaches to 10 digit

Add leading zeros in Python pandas (preceding zeros in data frame) 2

 

 

ADD leading Zeros in python pandas to the character column:

Create a simple dataframe with character columns:

# create dataframe

import pandas as pd
d = {'Col1' : ["1","200","3000","40000"]}
df=pd.DataFrame(d)
df

col1 of the dataframe is Character column

Add leading zeros in Python pandas (preceding zeros in data frame) 1

 

Add the leading zeros to character column in Python pandas:

To Add the leading zeros to character we use zfill() function , with desired total length of the column is passed as an argument

## Add leading zeros to the character column in Python

df['Col2'] = df['Col1'].str.zfill(10)
df

zfill() function fills the character column with leading zeros.  zfill() function with length of 10 fills the character column with preceding zeros till  the total length reaches 10.

Add leading zeros in Python pandas (preceding zeros in data frame) 2

Add the leading zeros to character column using rjust() function

In the below example we will be adding zeros until we get 10 digits at start of the value, with the help of rjust() function.

#### add leading zeros of character column using rjust() function

df['Col2']=df['Col1'].str.rjust(10, "0")
df

so the resultant dataframe with padded zeros to length 10 will be

Add leading zeros in Python pandas (preceding zeros in data frame) 2

 

 

 

Add leading zeros the string in python – zfill() Method

zfill() Function in Python pads string on the left with zeros to the required width.


Syntax of zfill() Method in Python

str.zfill(width)

width – width of the string.  The Final width which we would get after filling zeros.

Example of zfill() Function in Python

zfill() function takes up the string and fills the string  with preceding zeros until the desired length is obtained. we have shown two different examples below to depict the example of zfill() function in python.


#zfill() for positive number string
number1="345"
print number1.zfill(4)

# zfill() for negative number string
number2="-675"
print number2.zfill(6)

so the output with preceding zeros filled will be

‘0345’
‘-00675’

 

 

leading and trailing zeros using rjust() and ljust() function:

 
#### rjust() for adding leading zeros 

number1="345" 
number1.rjust(7, '0')


###### Add trailing zeros ljust

number3="345.2" 
number3.ljust(7, '0')

In the first example we are using rjust() function to add leading zeros to the string to the total length of 7. In the second example we are using ljust() function to add trailing zeros after the decimal until we get the total length of 7 digit. so the result will be

‘0000345’
‘345.200’

 

Add leading zeros using format function : Method 1

Format function takes up the numeric value as argument and pads them with preceding zeros until the desired length is reached. Here in our case we will padding till length 10.

 
#### add leading zeros using format function

number1 = 345
number2 = -675

#Method 1
'{:010}'.format(number1)
'{:010}'.format(number2)

so the result will be

‘0000000345’
‘-000000675’

 

Add leading zeros using format function : Method 2

Format function takes up the numeric value as argument and pads them with preceding zeros until the desired length is reached. Here in our case we will padding till length 7.

 
#### add leading zeros using format function

number1 = 345
number2 = -675

#Method 2
print(format(number1, '07'))
print(format(number2, '07'))

so the result will be

‘0000345’
‘-000675’

 


Other Related Topics:

 

previous add leading zeros in python dataframe pandas                                                                                                                next add leading zeros in python dataframe pandas

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.