2016-12-15 4 views
1

に包まれたカスタムオブジェクトを印刷する際に、複数のラインを持っている__repr__から戻るには何が必要なのかは、文字列表現が複数行に印刷されたCube私はパンダオブジェクト

class Cube(object): 
    def __init__(self): 
     pass 

    def __repr__(self): 
     return "⧉ ⟦x⨯y⟧\nCUBE" 

cube = Cube() 

cube 

⧉ ⟦x⨯y⟧ 
CUBE 

私のカスタムクラスを考えます。私はそれが私にはないpd.DataFrame

pd.DataFrame([cube]) 

enter image description here


と1行

pd.Series(cube) 

0 ⧉ ⟦x⨯y⟧\nCUBE 
dtype: object 

同じことに印刷パンダpd.Seriesでラップしかし
\nを印刷します。私は改行が欲しい。

答えて

1

私はあなたの実際の質問に対する良い答えを持っていないが、私はデータフレームのHTMLテーブルのビジュアルの一部を変更したいと思っていた過去に、私はIPython.display.display_html()pd.DataFrame.to_html()の組み合わせを使用しました。理想的ではありませんが、少なくとも1つの潜在的な回避策があります。実行可能なソリューションです

from IPython.display import display_html 
display_html(pd.DataFrame([cube]).to_html().replace("\\n", "\n<br>").replace("CUBE", '<b style="color: red">CUBE</b>'), raw=True) 

enter image description here

+0

。私はおそらくさらに操作することができます。ありがとう。 – piRSquared