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

In this Tutorial we will learn how to add Leading Zeros or preceding zeros in data frame in python pandas with an example. We will be filling the integer or string with preceding zeros till the desired length is obtained using zfill() function. 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() function
  • add preceding and trailing zeros after decimal using rjust() and ljust() function 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)
print df

Which results in a dataframe as shown below.

Col1
1
200
3000
40000

Add the leading zeros to numeric column in Python pandas

## Add leading zeros to the integer column in Python

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

We will be taking a column of a dataframe Col1 and applying a format which adds preceding zeros and makes the length of the field to 10 digit as shown above so the output will be

Col1
0000000001
0000000200
0000003000
0000040000

 

 

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)
print df

Which results in a dataframe as shown below.

Col1
1
200
3000
40000

Add the leading zeros to character column in Python pandas

## Add leading zeros to the character column in Python

df['Col1']= map(lambda x: x.zfill(10), df['Col1'])
print df

with the help of zfill() function we will be filling the character column with leading zeros.  We will be taking a column of a dataframe Col1 and applying the zfill() function with length of 10. which will fill the character column with preceding zeros to the total length of 10. so resultant column will be

Col1
0000000001
0000000200
0000003000
0000040000

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['Col1']=df['Col1'].str.rjust(10, "0")
df

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

Col1
0000000001
0000000200
0000003000
0000040000

 

 

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

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


Syntax of zfill() Function 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’

 

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