在 PyCharm 启动时设置默认 Pandas 选项

Pet*_*cas 6 python pycharm pandas

希望为 Pycharm 中的任何项目创建我的 Pandas 启动选项。我有一个名为 Test 的项目,它包含三个模块。

Startup_Panda_Options.py用我需要的设置创建,然后创建__init__.py它以便在测试项目启动时加载。当我运行一些测试数据时,test.py它失败了,因为我得到NameError: name 'pd' is not defined这意味着__init__.py从未运行过。

第二个选项是将 Startup_Panda_Options.py 放入文件夹,C:\Users\peter\Documents\PyCharm\venv\Lib\site-packages\IPython\core\profile但出现了同样的问题。

第三个选项,在 PyCharm 中通过 File | 设置 | 工具 | 启动任务 | 我参考了该Startup_Panda_Options.py文件。

因此,根据此处的建议和文档进行了三次尝试。关于如何使这些选项起作用的任何想法?三个模块如下:

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()

    
Run Code Online (Sandbox Code Playgroud)

小智 1

您是否尝试过在 test.py 中导入“Import Startup_Panda_Options.Panda_Options”语句?