【问题标题】:Change default options in pandas更改熊猫中的默认选项
【发布时间】:2014-05-29 23:55:41
【问题描述】:

我想知道是否有任何方法可以更改熊猫的默认显示选项。我想在每次运行 python 时更改显示格式和显示宽度,例如:

pandas.options.display.width = 150

我看到默认值是硬编码在pandas.core.config_init 中的。熊猫有什么方法可以正确地做到这一点吗?或者如果没有,是否有某种方法可以设置 ipython 至少在每次导入熊猫时更改配置?我唯一能想到的就是制作我自己的 mypandas 库,该库用每次加载时发出的一些额外命令来包装 pandas。有更好的想法吗?

【问题讨论】:

    标签: python configuration pandas ipython


    【解决方案1】:

    看看the docs:

    使用python/ipython环境的启动脚本导入 pandas 和 set options 使处理 pandas 的效率更高。去做 这个,在启动目录中创建一个 .py 或 .ipy 脚本 所需的配置文件。启动文件夹位于默认文件夹中的示例 ipython 配置文件可以在以下位置找到:

    $IPYTHONDIR/profile_default/startup
    

    更多信息可以在 ipython 文档中找到。一个例子 pandas 的启动脚本如下所示:

    import pandas as pd
    pd.set_option('display.max_rows', 999)
    pd.set_option('precision', 5)
    

    (或使用新形式pd.options.display.max_rows = 999)。

    你还问过:

    --当我从 ipython 中导入 pandas 时,有没有办法只运行 pandas 代码?导入pandas需要相当长的时间,所以我宁愿不要每次启动一个新的ipython实例时都这样做

    作为一种解决方法,您可以在后台导入 pandas。见Import python modules in the background in REPL

    【讨论】:

    • 如果您正在寻找 IPYTHONDIR 的位置,默认目录是:~/.ipython/
    • 如果可以的话,我会投票给这 10 倍。希望我在 前找到这个。队友的欢呼声! (还要感谢@Guido。)
    【解决方案2】:

    这里描述的有iPython config files:

    # Most of your config files and extensions will probably start
    # with this import
    
    import IPython.ipapi
    ip = IPython.ipapi.get()
    
    # You probably want to uncomment this if you did %upgrade -nolegacy
    # import ipy_defaults
    
    import os
    import pandas
    
    
    def main():
    
        #ip.dbg.debugmode = True
        ip.dbg.debug_stack()
    
        # uncomment if you want to get ipython -p sh behaviour
        # without having to use command line switches
        import ipy_profile_sh
        import jobctrl
    
        # Configure your favourite editor?
        # Good idea e.g. for %edit os.path.isfile
    
        #import ipy_editors
    
        # Choose one of these:
    
        #ipy_editors.scite()
        #ipy_editors.scite('c:/opt/scite/scite.exe')
        #ipy_editors.komodo()
        #ipy_editors.idle()
        # ... or many others, try 'ipy_editors??' after import to see them
    
        # Or roll your own:
        #ipy_editors.install_editor("c:/opt/jed +$line $file")
    
    
        o = ip.options
        # An example on how to set options
        #o.autocall = 1
        o.system_verbose = 0
    
        #import_all("os sys")
        #execf('~/_ipython/ns.py')
    
    
        # -- prompt
        # A different, more compact set of prompts from the default ones, that
        # always show your current location in the filesystem:
    
        #o.prompt_in1 = r'\C_LightBlue[\C_LightCyan\Y2\C_LightBlue]\C_Normal\n\C_Green|\#>'
        #o.prompt_in2 = r'.\D: '
        #o.prompt_out = r'[\#] '
    
        # Try one of these color settings if you can't read the text easily
        # autoexec is a list of IPython commands to execute on startup
        #o.autoexec.append('%colors LightBG')
        #o.autoexec.append('%colors NoColor')
        o.autoexec.append('%colors Linux')
    
        pandas.options.display.width = 150
    
    
    # some config helper functions you can use
    def import_all(modules):
        """ Usage: import_all("os sys") """
        for m in modules.split():
            ip.ex("from %s import *" % m)
    
    def execf(fname):
        """ Execute a file in user namespace """
        ip.ex('execfile("%s")' % os.path.expanduser(fname))
    
    main()
    

    制作separate Python profiles 可能更好。 (代码未经测试)。

    【讨论】:

    • 好的,这是一个开始——当我从 ipython 中导入 pandas 时,有没有办法只运行 pandas 代码?导入pandas需要相当长的时间,所以我不想每次启动一个新的ipython实例时都这样做。
    • @Noah:好吧,如果你创建不同的配置文件,你可以创建一个“cust_pandas”配置文件,它会在 iPython 时加载带有你自定义的 pandas,省略它只会加载标准的 iPython。这是我能想到的最好的了。
    【解决方案3】:

    我能够通过实际进入 pandas 文件夹(使用 pandas.__file__ 找到)来解决此问题。在 pandas 文件夹中有一个包含 config_init.py 文件的核心文件夹。线条

    cf.register_option('large_repr', 'truncate', pc_large_repr_doc,
                           validator=is_one_of_factory(['truncate', 'info']))
    

    设置默认选项。所以你可以把第二个参数改成'info'

    cf.register_option('large_repr', 'info', pc_large_repr_doc,
                           validator=is_one_of_factory(['truncate', 'info']))
    

    然后默认情况下,如果数据框超过max_rowsmax_columns,pandas 将打印汇总表,您也可以在此文件中更改默认值。我不确定这是否安全,但它对我有用。

    【讨论】:

    • 是的,我正在尝试避免编辑 pandas 库,因为我在许多不同的计算机上运行 pandas,并且希望能够在更新出现时尽可能频繁地更新它跨度>
    • 明白了。这超出了我的范围。
    猜你喜欢
    • 2021-11-03
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多