I tried to print pandas DataFrame.
But it omitted rows and columns…
pandas.DataFrame
is useful to handle table format data.
We can import CSV or Excel data into dataframe and summarize it.
Then sometimes we want to know dataframe content in progress.
So we will do like following.
print(df)
We will use print
.
But it omits rows and columns when it has a lot of rows and columns.
If important parts are omitted, it is hard to test program.
How can we print all rows and columns ?
So today I will introduce about "How to print all rows and columns in pandas.DataFrame".
Prepare data
First, we have to prepare data for DataFrame.
In order to raise omitting, we prepare large size CSV that has 20 columns and 80 rows.
col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11,col12,col13,col14,col15,col16,col17,col18,col19,col20
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
Repeat its rows 80 times.
Then read it as pandas.DataFrame by read_csv()
.
And do print
.
import pandas as pd
df1 = pd.read_csv("pandas_print_all.csv")
print(df1)
# col1 col2 col3 col4 col5 ... col16 col17 col18 col19 col20
# 0 1 2 3 4 5 ... 16 17 18 19 20
# 1 1 2 3 4 5 ... 16 17 18 19 20
# 2 1 2 3 4 5 ... 16 17 18 19 20
# 3 1 2 3 4 5 ... 16 17 18 19 20
# 4 1 2 3 4 5 ... 16 17 18 19 20
# .. ... ... ... ... ... ... ... ... ... ... ...
# 75 1 2 3 4 5 ... 16 17 18 19 20
# 76 1 2 3 4 5 ... 16 17 18 19 20
# 77 1 2 3 4 5 ... 16 17 18 19 20
# 78 1 2 3 4 5 ... 16 17 18 19 20
# 79 1 2 3 4 5 ... 16 17 18 19 20
# [80 rows x 20 columns]
It showed first 5 columns and rows.
Then it omitted columns and rows.
We use this data today.
How to print all rows and columns in pandas.DataFrame
In order to check maximum printing rows and columns, we can see pandas.options.display.max_rows
or pandas.options.display.max_columns
.
print("pd.options.display.max_rows")
print(pd.options.display.max_rows)
# pd.options.display.max_rows
# 60
print("pd.options.display.max_columns")
print(pd.options.display.max_columns)
# pd.options.display.max_columns
# 0
pandas.options.display.max_rows
is 60
. It means that it can display 60 rows without omitting.
pandas.options.display.max_columns
showed 0
. It means that it displays as much in its width settings.
And in order to print all rows and columns, use pandas.set_option
.
pd.set_option('display.max_rows', 500)
print(df1)
# col1 col2 col3 col4 col5 ... col16 col17 col18 col19 col20
# 0 1 2 3 4 5 ... 16 17 18 19 20
# (Python displays all rows.)
# 64 1 2 3 4 5 ... 16 17 18 19 20
# 65 1 2 3 4 5 ... 16 17 18 19 20
# 66 1 2 3 4 5 ... 16 17 18 19 20
# 67 1 2 3 4 5 ... 16 17 18 19 20
# 68 1 2 3 4 5 ... 16 17 18 19 20
# 69 1 2 3 4 5 ... 16 17 18 19 20
# 70 1 2 3 4 5 ... 16 17 18 19 20
# 71 1 2 3 4 5 ... 16 17 18 19 20
# 72 1 2 3 4 5 ... 16 17 18 19 20
# 73 1 2 3 4 5 ... 16 17 18 19 20
# 74 1 2 3 4 5 ... 16 17 18 19 20
# 75 1 2 3 4 5 ... 16 17 18 19 20
# 76 1 2 3 4 5 ... 16 17 18 19 20
# 77 1 2 3 4 5 ... 16 17 18 19 20
# 78 1 2 3 4 5 ... 16 17 18 19 20
# 79 1 2 3 4 5 ... 16 17 18 19 20
Like this, we could change displayed columns and rows by pandas.set_option
.
Conclusion
Today I explained about "How to print all rows and columns in pandas.DataFrame".
Following points are important.
- To check maximum-display columns or rows, print
pd.options.display.max_columns
orpd.options.display.max_rows
. - To print all data, change
display.max_rows
ordisplay.max_columns
bypd.set_option
.
In order to show all columns and rows, we can change pandas settings.