
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
Pandas Series.str.ljust() Method
The Series.str.ljust() method in in Python Pandas library is used to pad the right side of strings in a Series or Index to a specified minimum width.
This is equivalent to the string method str.ljust() in Python. The method ensures that each string in the Series or Index has at least the specified width, padding with a specified fill character if necessary.
Syntax
Following is the syntax of the Pandas Series.str.ljust() method −
Series.str.ljust(width, fillchar=' ')
Parameters
The Series.str.ljust() method accepts the following parameters −
width − An integer specifying the minimum width of the resulting string. Additional characters will be filled with fillchar.
fillchar − A string specifying the additional character for filling. The default is a whitespace character.
Return Value
The Series.str.ljust() method returns a Series or Index of objects with right-padded strings.
Example 1
In this example, we demonstrate the basic usage of the Series.str.ljust() method by right-padding the strings in a Series to a width of 8 using the fill character '.'.
import pandas as pd # Create a Series of strings s = pd.Series(['dog', 'lion', 'panda']) # Display the input Series print("Input Series") print(s) # Right-pad the strings print("Series after calling ljust with width=8 and fillchar='.'") print(s.str.ljust(8, fillchar='.'))
When we run the above code, it produces the following output −
Input Series 0 dog 1 lion 2 panda dtype: object Series after calling ljust with width=8 and fillchar='.': 0 dog..... 1 lion.... 2 panda... dtype: object
Example 2
This example demonstrates how to use the Series.str.ljust() method to right-pad strings in a DataFrame's column to a width of 10 using the fill character '-'.
import pandas as pd # Create a DataFrame df = pd.DataFrame({'Animal': ['Python', 'Tutorial', 'panda'], 'Legs': [4, 4, 2]}) print("Input DataFrame:") print(df) # Right-pad the strings in the 'Animal' column df['Animal'] = df['Animal'].str.ljust(10, fillchar='-') print("DataFrame after applying ljust with width=10 and fillchar='-':") print(df)
Following is the output of the above code −
Input DataFrame: Animal Legs 0 Python 4 1 Tutorial 4 2 panda 2 DataFrame after applying ljust with width=10 and fillchar='-': Animal Legs 0 Python-- 4 1 Tutorial- 4 2 panda--- 2
Example 3
In this example, we apply the Series.str.ljust() method to right-pad the index labels of a DataFrame to a width of 10 using the fill character '*'.
import pandas as pd # Create a DataFrame with an Index df = pd.DataFrame({'Value': [1, 2, 3]}, index=['first', 'second', 'third']) # Display the Input DataFrame print("Input DataFrame:") print(df) # Right-pad the index labels of a DataFrame df.index = df.index.str.ljust(10, fillchar='*') # Display the Modified DataFrame print("Modified DataFrame:") print(df)
Output of the above code is as follows −
Input DataFrame: Value first 1 second 2 third 3 Modified DataFrame: Value first***** 1 second**** 2 third***** 3