How to Use zfill() Function in Pandas


Often you may want to pad a string in a pandas Series by prepending a certain number of ‘0’ characters.

The most efficient way to do so is by using the zfill() function, which is designed to perform this exact action.

The zfill() function uses the following syntax:

pandas.Series.str.zfill(width)

where:

  • width: The minimum length of the resulting string – strings with length less than width be prepended with ‘0’ characters.

It’s important to note that this function adds ‘0’ characters to the beginning of strings and not the end. This is a common mistake.

The following example shows how to use the zfill() function in practice with a pandas DataFrame.

Example: How to Use the zfill() Function in Pandas

Suppose we create the following pandas DataFrame that contains information about the total sales made by various employees at some company:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'employee': ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
                   'sales': ['120', '1450', '80', '75', '75', '138', '1200']})

#view DataFrame
print(df)

  employee  sales
0        A    120
1        B   1450
2        C     80
3        D     75
4        E     75
5        F    138
6        G   1200

Suppose that we would like to pad the values in the sales column so that each value in the column has a minimum width of 4.

We can use the zfill() function to accomplish this.

We can use the following syntax to do so:

#prepend '0' characters to strings in sales column with minimum width of 4
df['sales'].str.zfill(width=4)

0    0120
1    1450
2    0080
3    0075
4    0075
5    0138
6    1200
Name: sales, dtype: object

We can see that each value in the sales column now has a minimum width of 4.

For the values that already had a width of 4, no ‘0’ characters’ were prepended to them.

For example, we can see in the output:

  • 120 has been converted to 0120.
  • 1450 has remained 1450.
  • 80 has been converted to 0080.
  • 75 has been converted to 0075.

And so on.

Note that you can choose any value for the width that you would like.

For example, we could instead choose a value of 6, which means that each string in the column must have a minimum width of 6:

#prepend '0' characters to strings in sales column with minimum width of 6
df['sales'].str.zfill(width=6)

0    000120
1    001450
2    000080
3    000075
4    000075
5    000138
6    001200
Name: sales, dtype: object

We can see that each value in the sales column now has a minimum width of 6.

For example, we can see in the output:

  • 120 has been converted to 000120.
  • 1450 has remained 001450.
  • 80 has been converted to 000080.
  • 75 has been converted to 000075.

And so on.

It’s important to note that this function only works with string variables. If you attempt to use the function with a numeric column, you will receive a TypeError because the function only works with strings.

It’s also important to note that this function only works on pandas Series. If we attempt to use the function with an entire pandas DataFrame or with multiple columns at once then we will receive an error.

Note: You can find the complete documentation for the zfill() function in pandas here.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

How to Use the Rolling.apply() Function in Pandas
How to Use the nunique() Function in Pandas
How to Use the get_loc() Function in Pandas
How to Use idxmin() Function in Pandas

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *