【问题标题】:Is there another way to solve about pandas set option?有没有另一种方法来解决熊猫设置选项?
【发布时间】:2021-04-11 23:29:16
【问题描述】:

我正在分析一个数据框并想查看更详细的列表
但即使我从谷歌搜索了一些解决方案,
我不明白为什么结果没有改变。 有什么问题??

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

# Import data
df = df = pd.read_csv(r"C:\Users\Administrator\Desktop\medical.txt")

pd.set_option("display.max_rows", 50)
pd.set_option('display.max_columns', 15)

print(df)


          id    age  gender  height  weight  ap_hi  ap_lo  cholesterol  gluc  
0          0  18393       2     168    62.0    110     80            1     1   
1          1  20228       1     156    85.0    140     90            3     1   
2          2  18857       1     165    64.0    130     70            3     1   
3          3  17623       2     169    82.0    150    100            1     1   
4          4  17474       1     156    56.0    100     60            1     1   
     ...    ...     ...     ...     ...    ...    ...          ...   ...   
69995  99993  19240       2     168    76.0    120     80            1     1   
69996  99995  22601       1     158   126.0    140     90            2     2   
69997  99996  19066       2     183   105.0    180     90            3     1   
69998  99998  22431       1     163    72.0    135     80            1     2   
69999  99999  20540       1     170    72.0    120     80            2     1    

【问题讨论】:

  • 什么没有改变?你的问题是什么?您可以编辑问题以提供可重现的示例,以便用户可以复制代码并重新创建您遇到的问题。请看How to make good reproducible pandas examples
  • 如果你想要 50 行,你可以给 df.head(50) 。您具体在寻找什么?
  • 我的意思是,如果我将显示选项设置为从 (0~100) 到 (69900~69999) 的 200,我只想在数据框中看到几百行

标签: pandas jupyter-notebook jupyter


【解决方案1】:

如果有办法显示足够多的列

pd.set_option('display.width',1000)
or
pd.set_option('display.width',None)

但行可能是你唯一使用的

df.head(50) 
or
df.tail(50)

或跟随显示全部

pd.set_option("display.max_rows", None)

为什么设置没用:

第二个参数不是可以查看的最大行数,而是一个内部模板参数

  • 代码如下:
set_option = CallableDynamicDoc(_set_option, _set_option_tmpl)

CallableDynamicDoc:

class CallableDynamicDoc:
    def __init__(self, func, doc_tmpl):
        self.__doc_tmpl__ = doc_tmpl
        self.__func__ = func
 
    def __call__(self, *args, **kwds):
        return self.__func__(*args, **kwds)
 
    @property
    def __doc__(self):
        opts_desc = _describe_option("all", _print_desc=False)
        opts_list = pp_options_list(list(_registered_options.keys()))
        return self.__doc_tmpl__.format(opts_desc=opts_desc, opts_list=opts_list)

【讨论】:

    【解决方案2】:

    查看“常用选项”一章中的https://pandas.pydata.org/pandas-docs/stable/user_guide/options.html

    您可以看到,如果“max_rows”低于数据框中的总行数,那么它会像您的结果一样显示。

    下面是我给你的链接中有趣部分的副本:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-03
      • 2011-07-28
      • 1970-01-01
      • 2019-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多