Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

Pandas Series.str.ljust() Method



TheSeries.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 methodstr.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 PandasSeries.str.ljust() method −

Series.str.ljust(width, fillchar=' ')

Parameters

TheSeries.str.ljust() method accepts the following parameters −

  • width − An integer specifying the minimum width of the resulting string. Additional characters will be filled withfillchar.

  • fillchar − A string specifying the additional character for filling. The default is a whitespace character.

Return Value

TheSeries.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 theSeries.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 stringss = pd.Series(['dog', 'lion', 'panda'])# Display the input Seriesprint("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 Series0      dog1     lion2    pandadtype: objectSeries after calling ljust with width=8 and fillchar='.':0    dog.....1    lion....2    panda...dtype: object

Example 2

This example demonstrates how to use theSeries.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 DataFramedf = 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  Legs0    Python     41  Tutorial     42     panda     2DataFrame after applying ljust with width=10 and fillchar='-':      Animal  Legs0    Python--     41  Tutorial-     42    panda---     2

Example 3

In this example, we apply theSeries.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 Indexdf = pd.DataFrame({'Value': [1, 2, 3]}, index=['first', 'second', 'third'])# Display the Input DataFrameprint("Input DataFrame:")print(df)# Right-pad the index labels of a DataFramedf.index = df.index.str.ljust(10, fillchar='*')# Display the Modified DataFrameprint("Modified DataFrame:")print(df)

Output of the above code is as follows −

Input DataFrame:        Valuefirst       1second      2third       3Modified DataFrame:            Valuefirst*****      1second****      2third*****      3
python_pandas_working_with_text_data.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp