至尊宝的传说
这是我找到的解决方案from IPython.display import displaydisplay(_df.style.set_properties(**{ 'width': '230px', 'max-width': '230px' }))我为此编写了一个辅助函数# decoratordef pandas_display(func): default_1 = pd.options.display.max_rows default_2 = pd.options.display.max_colwidth def wrapper(_df, exclude_cols=None, properties=None, limit_float=2): pd.options.display.max_rows = 1000 pd.options.display.max_colwidth = None func(_df, exclude_cols, properties, limit_float) pd.options.display.max_rows = default_1 pd.options.display.max_colwidth = default_2 return wrapper @pandas_displaydef display_df(self, exclude_cols, properties, limit_float): if exclude_cols is not None: if not isinstance(exclude_cols, list): exclude_cols = list(exclude_cols) cols = [x for x in self.columns.values if x not in set(exclude_cols)] self = self[cols] if not properties: properties = { 'text-align': 'left', 'white-space': 'pre-wrap', 'word-wrap': 'break-word', 'width': '230px', 'max-width': '230px', } a = self.style.set_properties(**properties) if limit_float is not None: a.format(thousands=',', precision=limit_float) display(a) d = display_dfpd.DataFrame.d = d你可以通过它打电话df.d()