更改pandas默认选项
我在想有没有办法改变pandas的默认显示选项。我想每次运行python的时候,能够改变显示格式和显示宽度,比如:
pandas.options.display.width = 150
我发现默认设置是写死在pandas.core.config_init
里的。有没有什么方法可以在pandas中正确地做到这一点?如果没有,至少有没有办法在每次导入pandas时设置ipython来改变这些配置?我能想到的唯一办法就是自己做一个mypandas库,在加载pandas的时候加上一些额外的命令。还有没有更好的主意呢?
3 个回答
我解决这个问题的方法是直接进入pandas的文件夹(可以通过 pandas.__file__
找到这个文件夹)。在pandas文件夹里,有一个叫做core的文件夹,里面有一个 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_rows
或者列数超过 max_columns
,pandas就会打印出一个总结表格。你也可以在这个文件里更改这些默认值。我不太确定这样做是否安全,但对我来说是有效的。
如这里所述,有一些iPython 配置文件:
# 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()
可能更好的是创建单独的 Python 配置文件。(这段代码没有经过测试)。
可以看看这个文档:
在python/ipython环境中使用启动脚本来导入pandas并设置选项,可以让使用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。可以参考在REPL中后台导入python模块。