The Pandas Series str function ljust() used to align left justify each elements of Series or Index by padding right side of strings. It returns Series or Index of objects that includes left justify string with padded.
1 Series.str.ljust(width, fillchar = ' ')
width : It is an integer that specifies minimum width of the resulting string. If value is less than specified length, it will be filled with fillchar.
fillchar : It is string that will be filled if string length is less than specified width. If not specified the default value will be whitespace.
1 import pandas as pd
2
3 ser = pd.Series(['String', 'Python', 'Test', 'Hello'])
4
5 print('The original Series object :')
6 print(ser)
7
8 res = ser.str.ljust(7, fillchar = '*')
9 print('The padded right side of Series element :')
10 print(res)
In the above example, a Series object is created by passing an array of string with different length. A ljust() function is called by passing a width and fill character that padded string right side, if string length is less than specified width. The result is assign to variable that will be printed on console.
1 The original Series object :
2 0 String
3 1 Python
4 2 Test
5 3 Hello
6 dtype: object
7 The padded right of Series element :
8 0 String*
9 1 Python*
10 2 Test***
11 3 Hello**
12 dtype: object
Example 2
1 import pandas as pd
2
3 ser = pd.Series(['String', 'Python', 'Test', 'Hello'])
4
5 print('The original Series object :')
6 print(ser)
7
8 res = ser.str.ljust(7, fillchar = '*')
9 print('The padded right side of Series element :')
10 print(res)
In the above example, a Series object is created by passing an array of string with different length. A ljust() function is called by passing a width and fill character that padded string right side, if string length is less than specified width. The result is assign to variable that will be print.
1 The original Series object :
2 0 welcome
3 1 to
4 2 IOGyan
5 dtype: object
6 The padded right side of Series element :
7 0 welcomexxx
8 1 toxxxxxxxx
9 2 IOGyanxxxx
10 dtype: object
Related options for your search