Pretty-print an entire Pandas Series / DataFrame
You can use the .head() method to print the first few rows of a Pandas Series or DataFrame in a "pretty" format.
To preview a Pandas Series or DataFrame, you can use the .head() method. It prints the first 5 rows by default, but you can pass a number as an argument to specify how many rows to show. For printing the entire DataFrame, see the methods below.
Python: Pretty-print an entire Pandas Series / DataFrame
import pandas as pd
# Create a sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
# Print the first few rows
print(df.head())
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Python - The Practical Guide</div>
You can also use the .style attribute to format the entire DataFrame using CSS. Note that in interactive environments like Jupyter, df.style renders automatically. In standard scripts, use .to_html() to output the formatted table.
Python: Use the style attribute to format the entire Pandas DataFrame with CSS
# Create a sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
# Use the .style attribute to format the DataFrame
print(df.style.to_html())For large DataFrames, you can use pd.set_option() to control how many rows and columns are displayed. Setting them to None will show the entire DataFrame.
Python: Display all rows and columns in a Pandas DataFrame
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
print(df)You can also use the .to_string() method to print the entire DataFrame in a single string:
Python: Convert a Pandas DataFrame to string
print(df.to_string())You can also use the .to_string() method with index=False and header=False to remove the index and header from the output:
Python: Convert a Pandas DataFrame to string without index and header
print(df.to_string(index=False, header=False))