Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas provide a method to add padding (whitespaces or other characters) to every string element in a series. .str has to be prefixed every time before calling this method to differentiate it from the Python’s default function otherwise, it will throw error.
Syntax: Series.str.pad(width, side=’left’, fillchar=’ ‘)
If width is less than or equal to length of string, no padding is added.
If width is more than the string length, the extra space is filled with whitespaces or passed character.
width: Minimum width of resulting string.
side: string input ( ‘left’, ‘right’ or ‘both’). Default is ‘left’. Padding will be added equally on respective side.
fillchar: Character to be padded. Default is ‘ ‘(White space).
Return Type: Series with added spaces/characters on the passed side of string
To download the CSV used in code, click here.
In the following examples, the data frame used contains data of some NBA players.
str.pad() method will be used to add padding to the text. The image of data frame before any operations is shown below:
Example #1: Left padding
In this example, a minimum length of string is set at 15 and whitespaces are added to left side of string in Team column using the
str.pad() method. Since white spaces can’t be seen, they are compared with custom input string and the result is checked if it’s True or not for team name “Boston Celtics” only.
As shown in the output image, the condition is True for team name Boston Celtics which means spaces were added successfully. Similarly the other strings are also padded according to their length.
Example #2: Right padding
In this example, a minimum length of string is set at 15 and ‘_’ are added to right side of string in Team column using the str.pad() method. ‘_’ is passed to fillchar parameters to add it instead of default whitespaces.
As shown in the output image, ‘_’ has been added to right side of string depending upon length of the string. After padding, the length of each string is 15.
Example 3: Both side padding
In this example, ‘+’ has been added to both side of string using fillchar parameter in str.pad(). The width parameter is set to 20, so that the length of each string after padding becomes same.
As shown in the output image, ‘+’ was added to both side of the string. The number of ‘+’ sign in each string may differ, but after padding the length of each string is 20.
Note: As it can be seen in the image, if the string has odd number of places left (width – length), then the priority is given to right side. Hence, the one left character will be added to the right side.
As in the first row of name column, Length of string was 13. So 20-13 = 7. Hence 3 ‘+’ are added to left side and ‘4’ to the right.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!