pandas显示全部列_Pandas 使用小技巧 No 23

本文介绍了Pandas如何显示全部列的技巧。通过设置系统配置,可以调整DataFrame的显示行数和列数。默认情况下,Pandas会截断显示中间的列,但可以通过指定参数来完整显示所有列。示例中展示了创建一个20行30列的DataFrame,并进行设置以完整显示30列的方法。
摘要由CSDN通过智能技术生成

Pandas 使用小技巧 23:系统配置

如何print一次只显示指定行数,指定列数。

使用pd.set_option方法,演示过程如下。

首先创建一个 DataFrame:

import numpy as np
import pandas as pd

生成20行30列数据:

df = pd.DataFrame(np.random.randint(0,10,size=(20,30)))
df

默认打印显示:

158c41b6b345e2aa179318ab2bc80817.png

20 行,列数为 30列,只实际显示 20列,中间用 ...

修改设置,只显示5行3列:

pd.set_option("display.max_rows",5)
pd.set_option("display.max_columns",3)

再次打印 df,显示如下&#x

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Pandas 中,可以使用 `pd.options.display.max_columns` 参数来设置 DataFrame 显示的最大数。将该参数设置为 `None` 或者一个大于 DataFrame 数的数字,就可以显示全部列。 下面是一个例子: ```python import pandas as pd # 创建一个具有20的DataFrame df = pd.DataFrame({'col_' + str(i): range(5) for i in range(20)}) # 设置最大数为 None pd.options.display.max_columns = None # 显示DataFrame print(df) ``` 输出结果: ``` col_0 col_1 col_2 col_3 col_4 col_5 col_6 col_7 col_8 col_9 col_10 col_11 col_12 col_13 col_14 col_15 col_16 col_17 col_18 col_19 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 ``` ### 回答2: 在Python的pandas库中,可以通过设置display_options参数来显示全部列。display_options参数是pandas库的一个全局设置,可以用来控制显示数和宽度。 首先,我们可以通过以下代码导入pandas库: import pandas as pd 然后,我们可以使用pd.set_option()函数来设置display_options参数,以显示全部列。具体来说,我们需要设置display.max_columns参数为None,表示不限制显示的数量。代码如下: pd.set_option('display.max_columns', None) 接下来,我们可以读取数据并进行处理,然后使用pandas的dataframe的head()方法来显示数据的前几行。在这种情况下,由于我们设置了显示全部列的选项,所有都将被显示出来。代码如下: data = pd.read_csv('data.csv') print(data.head()) 通过上述步骤,我们可以显示全部列的数据。如果数据的数较多,可能会导致显示结果混乱。此时,我们可以使用dataframe的to_string()方法来将数据转换为字符串,并使用print()函数来打印字符串,以便更清晰地显示全部列的数据。代码如下: print(data.to_string()) 总之,通过设置display_options参数,我们可以在pandas显示全部列的数据。 ### 回答3: 要想让pandas显示全部列,可以通过以下几种方式: 1. 使用pandas的set_option函数设置显示的最大数为None,即不限制最大数。可以使用如下代码实现: ```python import pandas as pd pd.set_option('display.max_columns', None) ``` 这样设置之后,pandas显示全部的,不受数限制。 2. 通过修改pandas的默认配置文件来设置显示的最大数。可以编辑pandas配置文件pandas/_config/config.py,找到以下行: ```python # max_colwidth : int # The maximum width in characters of a column in the repr of # a pandas data structure. When the column overflows, a "..." # placeholder is embedded in the output. # max_columns : int # max_rows and max_columns are used in __repr__() methods to decide if # to_string() or info() is used to render an object to a string. In case # python/IPython is running in a terminal and pandas is installed in # text-mode (e.g. not currently using the Jupyter notebook) max_columns # is used. 'None' value means unlimited. [default: 15] [currently: 15] 'io.excel.xls.writer', # pd.options.display.max_columns = 20 ``` 将最后一行的max_columns的值设置为None,则表示不限制显示的最大数。 以上两种方式都可以实现让pandas显示全部列的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值