Python Pandas: How to Left-Align Columns in Pandas DataFrame Display
By default, Pandas right-aligns numeric columns and handles string alignment inconsistently, which can make text-heavy DataFrames difficult to read. When your data contains names, addresses, descriptions, or other text fields, explicit left-alignment produces a much cleaner and more readable output. The approach differs depending on whether you are working in a terminal, a Jupyter notebook, or exporting to a file.
In this guide, you will learn how to left-align DataFrame output for console display, Jupyter notebooks, HTML export, and how to set alignment globally for an entire session.
Console Output with to_string()
For terminal and console display, use the justify parameter of the to_string() method:
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'City': ['Paris', 'London', 'New York'],
'Age': [25, 30, 35]
})
print(df.to_string(justify='left', index=False))
Output:
Name City Age
Alice Paris 25
Bob London 30
Charlie New York 35
All columns, including the numeric Age column, are now left-aligned. The index=False parameter hides the row index for a cleaner look.
Keeping the Index Visible
If you want to display the index alongside left-aligned data, simply omit index=False:
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie'],
'City': ['Paris', 'London', 'New York'],
'Age': [25, 30, 35]
})
print(df.to_string(justify='left'))
Output:
Name City Age
0 Alice Paris 25
1 Bob London 30
2 Charlie New York 35
Jupyter Notebook and HTML with .style
In Jupyter notebooks, DataFrames render as HTML tables. Use the .style accessor to apply CSS-based alignment:
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob'],
'City': ['Paris', 'London'],
'Score': [95, 87]
})
# Left-align all columns
df.style.set_properties(**{'text-align': 'left'})
This returns a Styler object that renders with left-aligned text in the notebook output.
Aligning Only Specific Columns
When you want to left-align text columns while keeping numeric columns right-aligned, use the subset parameter:
df.style.set_properties(
subset=['Name', 'City'],
**{'text-align': 'left'}
)
Only the Name and City columns are affected. The Score column retains its default alignment.
Combining Multiple Style Properties
You can apply several CSS properties at once for more polished output:
styled = df.style.set_properties(**{
'text-align': 'left',
'font-family': 'monospace',
'white-space': 'nowrap'
})
The monospace font ensures consistent character widths, and nowrap prevents cell contents from wrapping to a new line.
The .style methods return a new Styler object and do not modify the original DataFrame. You can chain multiple style calls together for complex formatting.
Setting Global Display Options
To apply left-alignment to all DataFrames throughout a session without calling to_string() each time, use pd.set_option():
import pandas as pd
# Left-align column headers in console output
pd.set_option('display.colheader_justify', 'left')
# Other useful display options
pd.set_option('display.max_rows', 100)
pd.set_option('display.max_columns', 20)
pd.set_option('display.width', None) # Auto-detect terminal width
After setting these options, every print(df) call in the session will use left-aligned column headers.
To restore all options to their defaults:
pd.reset_option('all')
The display.colheader_justify option only affects column headers, not the data values themselves. For full left-alignment of both headers and data in the console, use df.to_string(justify='left') instead.
Exporting with Alignment
To a Text File
import pandas as pd
df = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Score': [95, 87]})
with open('output.txt', 'w') as f:
f.write(df.to_string(justify='left', index=False))
The resulting text file will have cleanly left-aligned columns, suitable for plain-text reports or log files.
To an HTML File
import pandas as pd
df = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Score': [95, 87]})
html = df.style.set_properties(**{'text-align': 'left'}).to_html()
with open('output.html', 'w') as f:
f.write(html)
The exported HTML retains the left-alignment styling, making it ready for embedding in web pages or email reports.
Quick Reference
| Context | Method |
|---|---|
| Console / Terminal | df.to_string(justify='left') |
| Jupyter Notebook | df.style.set_properties(**{'text-align': 'left'}) |
| Specific columns | .set_properties(subset=['col1'], **{'text-align': 'left'}) |
| HTML export | .style.set_properties(...).to_html() |
| Global headers | pd.set_option('display.colheader_justify', 'left') |
| Reset all options | pd.reset_option('all') |
- Use
df.to_string(justify='left')for console and text file output. - Use
df.style.set_properties(**{'text-align': 'left'})for Jupyter notebooks and HTML rendering, with thesubsetparameter when you want to align only specific columns. - For persistent alignment across all DataFrames in a session, configure global display options with
pd.set_option().