我有這個:test = ['hey\nthere']Output: ['hey\nthere']當我插入 DataFrame 時,它保持相同的方式:test_pd = pd.DataFrame({'salute': test})Output: salute0 hey\nthere但我希望它能正確顯示: salute0 hey there我怎么能這樣做呢?
1 回答

夢里花落0921
TA貢獻1772條經驗 獲得超6個贊
在 jupiter notebook / google colab 中工作時,可以使用 CSS 自定義:
import pandas as pd
from IPython.display import display
test = ['hey\nthere']
test_pd = pd.DataFrame({'salute': test})
display(test_pd.style.set_properties(**{
'white-space': 'pre-wrap'
}))
替代解決方案涉及用 HTML 元素替換換行符br:
from IPython.display import display, HTML
test = ['hey\nthere']
test_pd = pd.DataFrame({'salute': test})
def pretty_print(df):
return display( HTML( df.to_html().replace("\\n","<br>") ) )
pretty_print(test_pd)
添加回答
舉報
0/150
提交
取消