将 Pandas 样式应用于 Jupyter Notebook 中的所有数据框

Mar*_*sen 8 python dataframe pandas pandas-styles

在具有许多 Pandas Dataframe 的 Jupyter 笔记本中,有没有办法为所有数据框设置默认样式选项?本质上是为了避免样板。

例如,隐藏所有数据帧的索引或列。df.style.hide_index()

是的,我可以编写包装函数,但更多地考虑诸如pd.options.display之类的东西

小智 0

如果您使用大量数据框,您可以在工作区中找到数据框,然后将样式应用于每个数据框

workspace = dir() #gets all variables in workspace
for variable_name in workspace:
    excec( f'var_type=type({variable_name})' ) #since we have access to only the names of variables we'll use exec
    if var_type== pd.Dataframe():#check to see if the variable is of type dataframe
        #apply style here
Run Code Online (Sandbox Code Playgroud)