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. By default, it will print the first 5 rows, but you can pass a number as an argument to specify how many rows to print.
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())
Watch a video course
Python - The Practical Guide
You can also use the .style
attribute to format the entire DataFrame using 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
df.style
For large dataframe with many rows, you can also use pd.options.display.max_rows
and pd.options.display.max_columns
to limit the number of rows and columns to display.
pd.options.display.max_rows = 10
pd.options.display.max_columns = 10
df
You can also use the .to_string()
method to print the entire DataFrame in a single 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:
print(df.to_string(index=False, header=False))