Open In App

Align columns to Left in Pandas – Python

Improve
Improve
Like Article
Like
Save
Share
Report

Pandas library is useful for performing exploratory data analysis in Python. A pandas dataframe represents data in a tabular format. We can perform operations on the data and display it. In this article, we are going to align columns to the Left in Pandas. When we display the Dataframe, we can align the data in the columns as left, right, or center.

Python DataFrame.style.set_properties() Function Syntax

In order to align columns to the left in Pandas Dataframe, we use the dataframe.style.set_properties() function.

Syntax: Styler.set_properties(subset=None, **kwargs)

Parameters:

  • subsetIndexSlice: A valid slice for data to limit the style application to.
  • **kwargsdict: A dictionary of property, value pairs to be set for each cell.

Returns: selfStyler

Creating Sample DataFrame

The default is right alignment as we can see in the below example.

Python3




# Python code demonstrate creating
# DataFrame from dict and left aligning
import pandas as pd
 
# initialise data of lists.
data = {'Name' : ['Tania', 'Ravi',
                  'Surbhi', 'Ganesh'],
         
        'Articles' : [50, 30, 45, 33],
         
        'Location' : ['Kanpur', 'Kolkata',
                      'Kolkata', 'Bombay']}
 
# Create DataFrame
df = pd.DataFrame(data)
display(df)


Output:

     Name  Articles Location
0 Tania 50 Kanpur
1 Ravi 30 Kolkata
2 Surbhi 45 Kolkata
3 Ganesh 33 Bombay

Align Columns to Left in Pandas in Python

Below are the ways by which we can align columns to left in Pandas in Python:

  • Using dataframe.style.set_properties() Function
  • Using set_table_styles() Function
  • Using to_string() and Justify Parameter
  • Using formatting
  • Using applymap() Function

Align Columns to Left Using set_properties() Function

In this example, the Pandas DataFrame df is styled to left-align its columns using the style.set_properties() method. The resulting styled DataFrame is then displayed using the display() function.

Python3




left_aligned_df = df.style.set_properties(**{'text-align': 'left'})
display(left_aligned_df)


Output:

Name    Articles    Location
Tania 50 Kanpur
Ravi 30 Kolkata
Surbhi 45 Kolkata
Ganesh 33 Bombay

Align Left Columns Using set_table_styles() Function

If we want the column headers aligned left, we use the set_table_styles() function. Here, both the column headers and the content of all the columns are left-aligned.

Python3




left_aligned_df = df.style.set_properties(**{'text-align': 'left'})
 
left_aligned_df = left_aligned_df.set_table_styles(
    [dict(selector='th', props=[('text-align', 'left')])])
 
display(left_aligned_df)


Output:

Name    Articles    Location
Tania 50 Kanpur
Ravi 30 Kolkata
Surbhi 45 Kolkata
Ganesh 33 Bombay

Left Column Alignment Using to_string() and Justify Parameter

In this example, the DataFrame df is converted to a string representation with left-aligned columns using the to_string() method. The resulting string is then printed, followed by a newline for separation.

Python3




print(df.to_string(justify='left', index=False))
print("\n")


Output:

Name    Articles    Location
Tania 50 Kanpur
Ravi 30 Kolkata
Surbhi 45 Kolkata
Ganesh 33 Bombay

Align Left Column Using Formatting

In this example, each cell of the DataFrame df is formatted to a fixed width of 15 characters with left alignment using the applymap() method and a lambda function. The resulting DataFrame formatted_df2 is then displayed.

Python3




formatted_df2 = df.applymap(lambda x: f"{x:<15}")
display(formatted_df2)


Output:

Name    Articles    Location
Tania 50 Kanpur
Ravi 30 Kolkata
Surbhi 45 Kolkata
Ganesh 33 Bombay

Align Left Column Using applymap() Function

In this example, the style attribute of the DataFrame df is modified using the applymap() method with a lambda function. The lambda function applies a left-aligned text style to each cell in the specified columns. The resulting styled DataFrame styled_df3 is then displayed.

Python3




styled_df3 = df.style.applymap(
    lambda x: f"{'text-align: left;':<15}", subset=df.columns)
display(styled_df3)


Output:

Name    Articles    Location
Tania 50 Kanpur
Ravi 30 Kolkata
Surbhi 45 Kolkata
Ganesh 33 Bombay


Last Updated : 22 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads