更改 pandas 的默认选项 [英] Change default options in pandas

查看:101
本文介绍了更改 pandas 的默认选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

pandas.options.display.width = 150

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

解决方案

看看在REPL中将python模块导入后台.

I'm wondering if there's any way to change the default display options for pandas. I'd like to change the display formatting as well as the display width each time I run python, eg:

pandas.options.display.width = 150

I see the defaults are hard-coded in pandas.core.config_init. Is there some way in pandas to do this properly? Or if not, is there some way to set up ipython at least to change the config each time I import pandas? Only thing I can think of is making my own mypandas library that wraps pandas with some extra commands issued each time it's loaded. Any better ideas?

解决方案

Have a look at the docs:

Using startup scripts for the python/ipython environment to import pandas and set options makes working with pandas more efficient. To do this, create a .py or .ipy script in the startup directory of the desired profile. An example where the startup folder is in a default ipython profile can be found at:

$IPYTHONDIR/profile_default/startup

More information can be found in the ipython documentation. An example startup script for pandas is displayed below:

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

(or use the new form pd.options.display.max_rows = 999).

You also asked:

-- is there any way to only run the pandas code when I import pandas from within ipython? it takes quite a while to import pandas, so I'd rather not do it every time I launch a new ipython instance

As a workaround, you could import pandas in the background. See Import python modules in the background in REPL.

这篇关于更改 pandas 的默认选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆