Change default options in pandas

2024/10/12 13:25:39

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?

Answer

Have a look at the docs:

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

$IPYTHONDIR/profile_default/startup

More information can be found in the ipython documentation. An examplestartup 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.

https://en.xdnf.cn/q/69647.html

Related Q&A

python-messaging Failed to handle HTTP request

I am using the code below to try to send an MMS message with python-messaging https://github.com/pmarti/python-messaging/blob/master/doc/tutorial/mms.rst Although the connection seems to go smoothly I …

Plotting confidence and prediction intervals with repeated entries

I have a correlation plot for two variables, the predictor variable (temperature) on the x-axis, and the response variable (density) on the y-axis. My best fit least squares regression line is a 2nd or…

Saving and Loading of dataframe to csv results in Unnamed columns

prob in the title. exaple:x=[(a,a,c) for i in range(5)] df = DataFrame(x,columns=[col1,col2,col3]) df.to_csv(test.csv) df1 = read_csv(test.csv)Unnamed: 0 col1 col2 col3 0 0 a a c 1 …

Python: print specific character from string

How do I print a specific character from a string in Python? I am still learning and now trying to make a hangman like program. The idea is that the user enters one character, and if it is in the word…

Python AttributeError: module string has no attribute maketrans

I am receiving the below error when trying to run a command in Python 3.5.2 shell:Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32 Type "copyrig…

How to add attribute to class in python

I have: class A:a=1b=2I want to make as setattr(A,c)then all objects that I create it from class A has c attribute. i did not want to use inheritance

Number of occurrence of pair of value in dataframe

I have dataframe with following columns:Name, Surname, dateOfBirth, city, countryI am interested to find what is most common combination of name and surname and how much it occurs as well. Would be nic…

how do i dump a single sqlite3 table in python?

I would like to dump only one table but by the looks of it, there is no parameter for this. I found this example of the dump but it is for all the tables in the DB: # Convert file existing_db.db to SQL…

Django automatically create primary keys for existing database tables

I have an existing database that Im trying to access with Django. I used python manage.py inspectdb to create the models for the database. Currently Im able to import the models into the python shell h…

matplotlib.pyplot scatterplot legend from color dictionary

Im trying to make a legend with my D_id_color dictionary for my scatterplot. How can I create a legend based on these values with the actual color? #!/usr/bin/python import matplotlib.pyplot as plt f…