我有这个:
test = ['heynthere']
Output: ['heynthere']
当我插入DataFrame时,它保持不变:
test_pd = pd.DataFrame({'salute': test})
Output:
salute
0 heynthere
但我希望它能正确显示:
salute
0 hey
there
我怎么能这么做?
在jupiter笔记本电脑/谷歌colab中工作时,可以使用CSS自定义:
import pandas as pd
from IPython.display import display
test = ['heynthere']
test_pd = pd.DataFrame({'salute': test})
display(test_pd.style.set_properties(**{
'white-space': 'pre-wrap'
}))
另一种解决方案是将换行符替换为br
HTML元素:
from IPython.display import display, HTML
test = ['heynthere']
test_pd = pd.DataFrame({'salute': test})
def pretty_print(df):
return display( HTML( df.to_html().replace("\n","<br>") ) )
pretty_print(test_pd)