Python

How to print all rows and columns in pandas.DataFrame

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 check 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".

Author


Mid-carieer engineer (AI, system). Good at Python and SQL.

Advantage to read

You can understand about "How to print all rows and columns in pandas.DataFrame". Then you don't have to concern about programming with pandas.DataFrame.


Data

First, we have to prepare data for DataFrame.

In order to raise omitting, we should prepare large size CSV like 20 columns x 80 rows.

CSV

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

(x 80rows)


Then read it as pandas.DataFrame by read_csv().

And do print.

DATA

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
# ..   ...   ...   ...   ...   ...  ...    ...    ...    ...    ...    ...
# 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 rows x 20 columns]


It showed first 5 columns and rows.

Then it omitted data.

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.

SAMPLE

print("pd.options.display.max_rows")
print(pd.options.display.max_rows)

print("pd.options.display.max_columns")
print(pd.options.display.max_columns)

# pd.options.display.max_rows
# 60
# 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.

SAMPLE

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
# 実際は表示されているがブログが長くなるので中略
# 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


SAMPLE

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
# 実際は表示されているがブログが長くなるので中略
# 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


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.

Point

  • To check maximum-display columns or rows, print pd.options.display.max_columns or pd.options.display.max_rows.
  • To print all data, change display.max_rows or display.max_columns by pd.set_option.




There are some other articles about pandas.Dataframe.

If you interested in them, please read them.

If you felt this article is useful, please share.

にほんブログ村 IT技術ブログへ

-Python
-,

© 2022 ITips