OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Dispaly DataFrame in Jupyter Notebook along with other attributes

  • Thread starter Thread starter ignoring_gravity
  • Start date Start date
I

ignoring_gravity

Guest
If I just construct a pandas DataFrame in a Jupyter notebook, then the output looks nice:

Code:
frame = pd.DataFrame({'a': [1,2,3]})
frame

enter image description here

However, if I make my own class whose repr is a pandas.DataFrame, then that formatting is lost:

Code:
class Foo:
    def __init__(self, frame, bar):
        self.frame = frame
        self.bar = bar
    
    def __repr__(self):
        return repr(self.frame)

enter image description here

How can I define the repr of my class Foo, such that when I display it in a Jupyter Notebook, it'll look just like it would if it was a pandas.DataFrame?
<p>If I just construct a pandas DataFrame in a Jupyter notebook, then the output looks nice:</p>
<pre class="lang-py prettyprint-override"><code>frame = pd.DataFrame({'a': [1,2,3]})
frame
</code></pre>
<p><a href="https://i.sstatic.net/6l9Bz.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/6l9Bz.png" alt="enter image description here" /></a></p>
<p>However, if I make my own class whose <code>repr</code> is a <code>pandas.DataFrame</code>, then that formatting is lost:</p>
<pre class="lang-py prettyprint-override"><code>class Foo:
def __init__(self, frame, bar):
self.frame = frame
self.bar = bar

def __repr__(self):
return repr(self.frame)
</code></pre>
<p><a href="https://i.sstatic.net/ncr8n.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ncr8n.png" alt="enter image description here" /></a></p>
<p>How can I define the <code>repr</code> of my class <code>Foo</code>, such that when I display it in a Jupyter Notebook, it'll look just like it would if it was a <code>pandas.DataFrame</code>?</p>
 
Top