w3resource

Pandas Series: str.pad() function

Series-str.pad() function

The str.pad() function is used to pad strings in the Series/Index up to width.

Syntax:

Series.str.pad(self, width, side='left', fillchar=' ')
Pandas Series: str.pad() function

Parameters:

Name Description Type/Default Value Required / Optional
width  Minimum width of resulting string; additional characters will be filled with character defined in fillchar. int Required
side  Side from which to fill resulting string. {'left', 'right', 'both'}
Default Value: 'left'
Required
fillchar  Additional character for filling, default is whitespace. str
Default Value: ' '
Required

Returns: Series or Index of object
Returns Series or Index with minimum number of char in object.

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(["tiger", "lion"])
s

Output:

0    tiger
1     lion
dtype: object

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(["tiger", "lion"])
s.str.pad(width=10) 

Output:

0         tiger
1          lion
dtype: object

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(["tiger", "lion"])
s.str.pad(width=10, side='right', fillchar='-')

Output:

0    tiger-----
1    lion------
dtype: object

Python-Pandas Code:

import numpy as np
import pandas as pd
s = pd.Series(["tiger", "lion"])
s.str.pad(width=10, side='both', fillchar='-')

Output:

0    --tiger---
1    ---lion---
dtype: object
Pandas Series: str.pad() function

Previous: Series-str.lstrip() function
Next: Series-str.partition() function



Follow us on Facebook and Twitter for latest update.