简体   繁体   中英

Set default Pandas options at Startup in PyCharm

Looking to create my Pandas startup options for any project in Pycharm. I have a project called Test which has three modules.

I created Startup_Panda_Options.py with the settings I require, then created __init__.py so it would be loaded when Test project starts up. When I run on some test data, in test.py it fails as I'm getting NameError: name 'pd' is not defined which implies __init__.py never ran.

The second option was placing Startup_Panda_Options.py into folder C:\Users\peter\Documents\PyCharm\venv\Lib\site-packages\IPython\core\profile but the same issue arises.

Third option, in PyCharm via File | Settings | Tools | Startup Tasks | I referenced the Startup_Panda_Options.py file.

So three attempts based on suggestions from here and the documentation. Any ideas on how to get any of these options working? The three modules are below:

Startup_Panda_Options.py:

import pandas as pd

def Panda_Options():
        options = {
            'display': {
                'max_columns': None,     # used in __repr__() methods ‘None’ value means unlimited.
                'max_colwidth': 25,     # sets the maximum width of columns. Cells of this length or longer will be truncated with an ellipsis.
                'expand_frame_repr': False, # Don't wrap to multiple pages
                'max_rows': None,      # This sets the maximum number of rows pandas should output: ‘None’ value means unlimited.
                'max_seq_items': 50,     # Max length of printed sequence
                'precision': 4,       # sets the output display precision in terms of decimal places.
                'show_dimensions': False   # Whether to print out dimensions at the end of DataFrame repr. If ‘truncate’ is specified, only print out the dimensions if the frame is truncated (e.g. not display all rows and/or columns)
            },
            'mode': {
                'chained_assignment': None  # Controls SettingWithCopyWarning: ‘raise’, ‘warn’, or None. Raise an exception, warn, or no action if trying to use chained assignment i.e. If try to change the value of a copy of the datafrrame as opposed to the dataframe itself
            }
        }

        # Loop through dictionaries
        for category, option in options.items():
            # Inside the nested dictionaries
            for op, value in option.items():
                # Set Pandas options usinf f strings
                pd.set_option(f'{category}.{op}', value)

if __name__ == '__main__':
    Panda_Options()
    del Panda_Options # Clean up namespace in the interpreter

__init__.py:
Import Startup_Panda_Options.Panda_Options

test.py:      
def main():
    url = ('https://archive.ics.uci.edu/ml/machine-learning-databases/abalone/abalone.data')
    cols = ['sex', 'length', 'diam', 'height', 'weight', 'rings']
    abalone = pd.read_csv(url, usecols=[0, 1, 2, 3, 4, 8], names=cols)
    print(abalone)

if __name__ == "__main__":
    main()

Have you tried importing the "Import Startup_Panda_Options.Panda_Options" statement in your test.py.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM