Noa*_*oah 3 python configuration ipython pandas
我想知道是否有任何方法可以更改熊猫的默认显示选项。我想在每次运行python时更改显示格式以及显示宽度,例如:
pandas.options.display.width = 150
Run Code Online (Sandbox Code Playgroud)
我看到默认值是在中硬编码的pandas.core.config_init。熊猫有办法正确地做到这一点吗?或者,如果没有,是否有某种方法可以设置ipython至少在每次导入熊猫时更改配置?我唯一能想到的就是制作自己的mypandas库,该库用每次加载时发出的一些额外命令包装大熊猫。还有更好的主意吗?
看一下文档:
在python / ipython环境中使用启动脚本来导入熊猫并设置选项可以使使用熊猫更加高效。为此,请在所需配置文件的启动目录中创建.py或.ipy脚本。可以在以下位置找到启动文件夹位于默认ipython配置文件中的示例:
Run Code Online (Sandbox Code Playgroud)$IPYTHONDIR/profile_default/startup可以在ipython文档中找到更多信息。下面显示了熊猫的启动脚本示例:
Run Code Online (Sandbox Code Playgroud)import pandas as pd pd.set_option('display.max_rows', 999) pd.set_option('precision', 5)
(或使用新表格pd.options.display.max_rows = 999)。
您还问:
-从ipython导入熊猫时,有什么方法只能运行熊猫代码吗?导入熊猫需要花费相当长的时间,所以我宁愿每次启动新的ipython实例时也不要这样做
解决方法是,您可以在后台导入熊猫。请参阅在REPL中在后台导入python模块。
如此处所述,有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()
Run Code Online (Sandbox Code Playgroud)
制作单独的 Python 配置文件可能会更好。(该代码未经测试)。