search
Search
Login
Math ML Join our weekly DS/ML newsletter
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
brightness_medium
share
arrow_backShare
Twitter
Facebook

Pandas Series str | pad method

Pandas
chevron_right
Documentation
chevron_right
Series
chevron_right
String Operations
schedule Jul 1, 2022
Last updated
local_offer PythonPandas
Tags

Pandas Series.pad(~) pads each string of the Series until the specified length is reached.

WARNING

If the value is not of type string, then NaN will be returned for that value. If all the values are not of type string, then an error will be thrown.

Parameters

1. width | int

The desired length to pad until. If a string is larger than the specified width, then that string is left as is.

2. side | string | optional

The side of the string to pad:

Value

Description

"left"

Adds padding to the left.

"right"

Adds padding to the right.

"both"

Adds padding to both sides.

By default, side="left".

3. fillchar | string | optional

The character to pad with. By default, fillchar=" " (a single whitespace).

Return Value

A Series object.

Examples

Basic usage

To left-pad with "z" until a width of 5:

s = pd.Series(["ab", "2", 2])
s.str.pad(width=5, fillchar="z") # side="left"
0 zzzab
1 zzzz2
2 NaN
dtype: object

Notice how the integer 2 was turned into NaN - this is because it is not a string.

Specifying side=both

Passing side="both" pads both ends of each string:

s = pd.Series(["ab", "2"])
s.str.pad(width=5, fillchar="z", side="both")
0 zzabz
1 zz2zz
dtype: object

Here, notice how two zs were added to the left of "ab", and only one z to the right.

Undefined padding for uneven cases

Unfortunately, there is no rule where the fillchar always gets added to the left side first. For instance, consider the following:

s = pd.Series(["A"])
s.str.pad(width=6, fillchar="z", side="both")
0 zzAzzz
dtype: object

In this case, we have one additional z on the right as opposed to the left.

Case when overflow

When a string is larger than the specified width, then it is left as is:

s = pd.Series(["abbbbb", "20000"])
s.str.pad(width=2, fillchar="z", side="both")
0 abbbbb
1 20000
dtype: object
mail
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!