1 回答
TA贡献1789条经验 获得超10个赞
这是我找到的解决方案
from IPython.display import display
display(_df.style.set_properties(**{
'width': '230px',
'max-width': '230px'
}))
我为此编写了一个辅助函数
# decorator
def 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_display
def 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_df
pd.DataFrame.d = d
你可以通过它打电话df.d()
添加回答
举报