在Jupyter Notebook上显示Pandas数据帧中的多个空格的方法



在笔记本上显示熊猫数据帧对象时,多个空格显示为单个空格。而且我无法复制多个空格。

我想按原样显示所有空格并复制其中的一串。

有什么办法吗?

display(pd.DataFrame([["ab c", "ab  c"], ["ab   c", "ab    c"]]))

实际

0     1   
0   ab c  ab c
1   ab c  ab c

我的期望

0       1     
0   ab c    ab  c
1   ab   c  ab    c

此问题已在不同的上下文中得到解答:

在 Jupyter 笔记本中保留熊猫数据帧显示的额外空格

def print_df(df):
return df.style.set_properties(**{'white-space': 'pre'})
print_df(df)

最新更新