JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

How to pretty print an entire Pandas Series/DataFrame

Author:JIYIK Last Updated:2025/05/02 Views:

We will introduce various methods to pretty print the entire Pandas Series/DataFrame, such as option_context, set_option, and options.display.


option_contextPretty Printing Pandas DataFrame

We can option_contextuse with one or more options:

# python 3.x
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 10))
with pd.option_context(
    "display.max_rows", None, "display.max_columns", None
):  # more options can be specified also
    print(df)

Output:

          0         1         2         3         4         5         6  \
0  0.536629 -0.317664  1.109011 -0.690963  0.895591 -2.008769 -0.323554   
1  1.978043  1.179393 -0.923501 -1.086199  1.311835  0.281665  0.132701   
2 -0.119101  0.339344  0.445792  0.544024 -0.780646 -0.505816  0.709910   
3 -0.092554 -1.547887  0.895174  0.998706 -0.480896 -0.969276 -0.050267   
4 -0.433900 -0.242806  2.435337 -0.299294 -1.011233  0.556583 -1.466501   

          7         8         9  
0 -2.454773  1.477553 -1.274452  
1  0.524635 -0.707736 -0.283512  
2  1.381855  1.676523 -0.206820  
3 -2.183197 -0.061560 -0.295834  
4  0.168427 -0.278612 -0.812824 

set_option()Can be displayed without being truncated

To display DataFramethe full content of , we need to set the following 4 options:

# python 3.x
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 10))
pd.set_option("display.max_rows", None)
pd.set_option("display.max_columns", None)
pd.set_option("display.width", None)
pd.set_option("display.max_colwidth", -1)
print(df)

Output:

          0         1         2         3         4         5         6         7         8         9
0 -0.137170  0.491314  0.213574  0.794829  0.244942 -0.472234  0.090120 -0.349087  1.496215 -0.674469
1  1.575921 -0.447399 -1.254165  1.865231 -0.112770  0.535931 -1.092489  0.032282 -0.501178 -0.521661
2  1.390534  0.147977 -0.908857  0.876250 -0.260842  0.873867  1.199424 -1.058784  0.278643  1.623892
3 -0.277710 -0.137027 -0.347331 -0.133104  0.495571  0.770674  0.819736 -0.130426  0.556820 -0.599270
4  0.146928  0.726470 -0.831788 -0.922454 -0.835223  0.494688  0.182850 -0.916735  0.678326 -0.953774

Used to display DataFramelargeoptions.display

As display.contextan alternative to , we set the option like this to display large DataFrame:

# python 3.x
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(5, 10))


def set_pandas_display_options() -> None:
    display = pd.options.display
    display.max_columns = 100
    display.max_rows = 100
    display.max_colwidth = 199
    display.width = None


set_pandas_display_options()
print(df)

Output:

          0         1         2         3         4         5         6         7         8         9
0 -1.915164 -1.953066  0.279874 -1.841046  0.105823 -2.070710 -0.075651 -0.110299  0.892385 -1.270936
1  0.135022 -0.088581 -0.257854  0.167365  1.593467 -0.131887  0.780474 -0.394496 -1.942498  0.192672
2 -0.276460  0.225853 -0.896788 -0.639678  1.867691 -1.457160 -0.902934 -0.546596  0.928684 -0.041108
3  0.851837  0.526239  1.187710  0.139416  0.546204  0.409457  0.542819 -1.174244 -3.118918  0.086719
4  0.402529 -0.724139 -0.294063 -0.725560  1.159756  0.493684  0.277930  0.384105 -0.475742  0.675826

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to Convert DataFrame Column to String in Pandas

Publish Date:2025/05/02 Views:161 Category:Python

We will look at methods for converting Pandas DataFrame columns to strings. Pandas Series.astype(str) Method DataFrame.apply() Methods operate on the elements in a column We will use the same DataFrame below in this article. import pandas a

How to count the frequency of values in a Pandas DataFrame

Publish Date:2025/05/02 Views:84 Category:Python

Sometimes, when you use DataFrame , you may want to count the number of times a value occurs in a column, or in other words, calculate the frequency. There are mainly three methods used for this. Let's look at them one by one. df.groupby().

How to get value from Pandas DataFrame cell

Publish Date:2025/05/02 Views:147 Category:Python

We'll look at using to get values ​​from cells in iloc Pandas , which is great for selecting by position, and how it differs from . We'll also learn about the and methods, which we can use when we don't want to set the return type to .

How to Add a Row to a Pandas DataFrame

Publish Date:2025/05/02 Views:127 Category:Python

Pandas is designed to load a fully populated DataFrame . We can pandas.DataFrame add them one by one in . This can be done by using various methods, such as .loc , dictionary, pandas.concat() or DataFrame.append() . .loc [index] Add rows to

How to change the order of Panas DataFrame columns

Publish Date:2025/05/02 Views:184 Category:Python

We will show how to use insert and reindex to change the order of columns in different ways pandas.DataFrame , such as assigning column names in a desired order. pandas.DataFrame Sort the columns in the new order The easiest way is columns

Convert Pandas to CSV without index

Publish Date:2025/05/01 Views:159 Category:Python

As you know, an index can be thought of as a reference point used to store and access records in a DataFrame. They are unique for each row and usually range from 0 to the last row of the DataFrame, but we can also have serial numbers, dates

Convert Pandas DataFrame to Dictionary

Publish Date:2025/05/01 Views:198 Category:Python

This tutorial will show you how to convert a Pandas DataFrame into a dictionary with the index column elements as keys and the corresponding elements of other columns as values. We will use the following DataFrame in the article. import pan

Convert Pandas DataFrame columns to lists

Publish Date:2025/05/01 Views:192 Category:Python

When working with Pandas DataFrames in Python, you often need to convert the columns of the DataFrame into Python lists. This process is very important for various data manipulation and analysis tasks. Fortunately, Pandas provides several m

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial