44

I tried this code, expecting it to use IPython's display function:

import pandas as pd
data = pd.DataFrame(data=[tweet.text for tweet in tweets], columns=['Tweets'])
 
display(data.head(10))

But I get an error message that says NameError: name 'display' undefined. Why? How do I make it so that I can use display?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Matt Wonderwall
  • 551
  • 1
  • 4
  • 8
  • https://docs.python.org/3/tutorial/inputoutput.html – cs95 Mar 16 '18 at 19:47
  • The normal Python3 display is : `print(data)`. Or in an interactive session just type `data`. – hpaulj Mar 16 '18 at 21:10
  • 2
    This question became relevant when I used VSCode to convert .ipynb to .py. The line `from IPython.display import display` was needed. – BSalita Jun 15 '20 at 19:33

2 Answers2

59

display is a function in the IPython.display module that runs the appropriate dunder method to get the appropriate data to ... display. If you really want to run it

from IPython.display import display
import pandas as pd

data = pd.DataFrame(data=[tweet.text for tweet in tweets], columns=['Tweets'])

display(data.head(10))

But don't. IPython is already doing that for you. Just do:

data.head(10)

You even might have IPython uninstalled, try:

pip install IPython

or if running pip3:

pip3 install IPython
micstr
  • 5,080
  • 8
  • 48
  • 76
piRSquared
  • 285,575
  • 57
  • 475
  • 624
  • Shouldn't the solution be on top to avoid confusion? "IPython is already doing that for you. Just do:..." – LabGecko May 28 '19 at 10:15
  • 3
    Under Jupyter, `display` is quite use full as its notably if you use `pandas`, when you need to display multiple objets: `display`produces the full representation where `print` bings only the text one. – hpchavaz Feb 01 '22 at 08:49
5

To solve the problem on pycaret, you have to open the below file -

..\env\Lib\site-packages\pycaret\datasets.py

and add the line of code -

from IPython.display import display
Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
guido albani
  • 51
  • 1
  • 1