Align columns to Left in Pandas – Python
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.
The default is right alignment as we can see in the below example.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
In order to align columns to left in pandas dataframe, we use the dataframe.style.set_properties() function.
Syntax: Styler.set_properties(subset=None, **kwargs)
- 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.
In the above example, the content of all columns are left-aligned, except the column headers. The column headers are center-aligned.
If we want the column headers aligned left, we use the set_table_styles() function.
In the above example, both the column headers and the content of all the columns are left-aligned.