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:
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 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 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 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 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 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
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
‘-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
‘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
‘-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
‘-000675’
Other Related Topics:
- join string in pandas dataframe
- Get string length of pandas dataframe
- startswith() function in pandas dataframe
- Swap the case in pandas dataframe
- Convert to uppercase – pandas dataframe
- Convert to lowercase – pandas dataframe
- Convert to propercase – pandas dataframe
- String Replace in pandas dataframe
- Check for only lower cases in pandas dataframe
- Check for only upper cases in Pandas dataframe
- Check for title case in Pandas dataframe